跳到正文

题解

追忆似水华年

psiggy约 31 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 31 分钟
获赞
6
题目
P12826

版本

  1. v1当前

注意到 bi,ib_{i,i} 可以确定节点 ii 的值 aia_i 并且有 bi,j=ai⊕ajb_{i,j}=a_i\oplus a_j,然后其实这道题目就没有然后了。

具体实现步骤就是,首先输入 bi,jb_{i,j} 然后把 aia_i 赋值为 bi,ib_{i,i} 然后我们遍历每个 i,j (i<j)i,j\ (i<j) 如果 bi,j=ai⊕ajb_{i,j}=a_i\oplus a_j 我就在 i,ji,j 之间加一条边,最后输出即可。

然后我们来证明这个算法的正确性。

相邻节点 ii 和 jj 之间的路径只包含这两个节点,因此路径的异或和就是 ai⊕aja_i\oplus a_j。非相邻节点的路径异或和会包含中间节点,不会满足 bi,j=ai⊕ajb_{i,j}=a_i\oplus a_j。

最后分析一下复杂度,每次需要 O(n2)O(n^2) 遍历,所以总时间复杂度为 O(T×n2)O(T\times n^2)。似乎目前最优解。

上代码:

C++
#include<bits/stdc++.h>
#define I using
#define AK namespace
#define IOI std
#define i_ak return
#define ioi  0
#define i_will signed
#define ak main
#define IMO ()
#define double long double
#define R register
#define fi first
#define se second
#define mem memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
#define pb emplace_back
#define sort stable_sort
#define con continue
#define br break
I AK IOI;
namespace BigINT{
	#define digit short
    #define ull unsigned long long
    #define ll long long
    class bigInt {digit *value;ull capacity;ull length;bool sign;private:inline void Genshin_Impact_start();inline void emplace_back(const digit &new_digit);inline void put_string(std::string from);inline void put_bigInt(const bigInt &from);inline void put_ull_interger(ull from, const bool &new_sign = 0);inline void put_ll_interger(const ll &from);inline void pop_zero();inline void negation();inline digit at(const int &idx) const;public:void reserve(ull new_cap);bigInt& operator =(const bigInt &from);bigInt& operator =(std::string from);bigInt& operator =(const ull &from);bigInt& operator =(const ll &from);bigInt& operator =(const unsigned int &from);bigInt& operator =(const int &from);bigInt() { Genshin_Impact_start(); }bigInt(std::string str) { put_string(str); }bigInt(const ull &from) { put_ull_interger(from, 0); }bigInt(const ll &from) { put_ll_interger(from); }bigInt(const unsigned int &from) { put_ull_interger(from, 0); }bigInt(const int &from) { put_ll_interger(from); }~bigInt() {delete[] value;}friend std::ostream& operator<<(std::ostream& out, const bigInt &num) {if (num.length == 0) {out << '0';return out;}if (num.sign == 1) out << '-';for (int idx = (num.length) - 1; idx >= 0; --idx) out << char(num.value[idx] + 48);return out;}friend std::istream& operator>>(std::istream& in, bigInt &num) {num.Genshin_Impact_start();char ch = in.get();while (ch < '0' || ch > '9') {if (ch == '-') num.sign = 1;ch = in.get();}while (ch >= '0' && ch <= '9') {num.emplace_back(ch ^ 48);ch = in.get();}for (int l = 0, r = num.length - 1; l < r; ++l, --r) std::swap(num.value[l], num.value[r]);num.pop_zero();return in;}inline ull size();inline ll to_digit();inline std::string to_string();inline bool operator==(const bigInt &other) const;inline bool operator<(const bigInt &other) const;inline bool operator>(const bigInt &other) const;inline bool operator<=(const bigInt &other) const;inline bool operator>=(const bigInt &other) const;inline bool operator!=(const bigInt &other) const;inline bigInt operator<<(const int &num);inline bigInt operator>>(const int &num);inline bigInt operator-() const;inline bigInt operator+(bigInt other);inline bigInt operator-(bigInt other);inline bigInt operator*(bigInt other);inline bigInt operator/(bigInt other);inline bigInt operator%(bigInt other);inline bigInt operator&(bigInt other);inline bigInt operator|(bigInt other);inline bigInt &operator<<=(const int &num);inline bigInt &operator>>=(const int &num);inline bigInt &operator+=(const bigInt &other);inline bigInt &operator-=(const bigInt &other);inline bigInt &operator*=(const bigInt &other);inline bigInt &operator/=(const bigInt &other);inline bigInt &operator%=(const bigInt &other);inline bigInt &operator&=(const bigInt &other);inline bigInt &operator|=(const bigInt &other);bigInt& operator++();bigInt operator++(int);bigInt& operator--();bigInt operator--(int);};inline void bigInt::Genshin_Impact_start() {this->sign = 0;this->capacity = 1;this->length = 0;this->value = new digit[1];this->reserve(-1);}void bigInt::reserve(ull new_cap = -1) {if (new_cap == -1) new_cap = (this->capacity) << 1;digit *past_value = this->value;this->capacity = new_cap;this->value = new digit[this->capacity];for (int idx = 0; idx < (this->length); ++idx) value[idx] = past_value[idx];for (int idx = (this->length); idx < new_cap; ++idx) { value[idx] = 0; }delete[] past_value;}inline void bigInt::put_string(std::string from) {this->Genshin_Impact_start();if (from.front() == '-') {sign = 1;from = from.substr(1);}for (const char &to_put : from) {this->emplace_back(to_put ^ 48);}for (int l = 0, r = this->length - 1; l < r; ++l, --r) std::swap(value[l], value[r]);this->pop_zero();}inline void bigInt::put_bigInt(const bigInt &from) {this->Genshin_Impact_start();for (int idx = 0; idx < from.length; ++idx) this->emplace_back(from.value[idx]);this->sign = from.sign;}inline void bigInt::put_ull_interger(ull from, const bool &new_sign) {this->Genshin_Impact_start();this->sign = new_sign;while (from) {this->emplace_back(from % 10);from /= 10;}this->pop_zero();}inline void bigInt::put_ll_interger(const ll &from) {if (from < 0) this->put_ull_interger((ull)-from, 1);else this->put_ull_interger(from, 0);}inline void bigInt::emplace_back(const digit &new_digit) {if ((this->length) == (this->capacity)) this->reserve();this->value[(this->length)++] = new_digit;}inline void bigInt::pop_zero() {while (length && this->value[length - 1] == 0) {--length;if (length == (capacity >> 1)) reserve(capacity >> 1);}}inline void bigInt::negation() { this->sign = !(this->sign); }inline digit bigInt::at(const int &idx) const {if (idx < length) return value[idx];else return 0;}bigInt &bigInt::operator=(const bigInt &from) { this->put_bigInt(from); return *this; }bigInt &bigInt::operator=(std::string from) { this->put_string(from); return *this; }bigInt &bigInt::operator=(const ull &from) { put_ull_interger(from, 0); return *this; }bigInt &bigInt::operator=(const ll &from) { put_ll_interger(from); return *this; }bigInt &bigInt::operator=(const unsigned int &from) { put_ull_interger(from, 0); return *this; }bigInt &bigInt::operator=(const int &from) { put_ll_interger(from); return *this; }inline ull bigInt::size() { return this->length; }inline ll bigInt::to_digit() {ll ans = 0, weight = 1;for (int i = 0; i < length; ++i) {ans += value[i] * weight;weight *= 10;}return (sign ? (-ans) : ans);}inline std::string bigInt::to_string() {std::string ans;if (sign) ans.push_back('-');for (int i = length - 1; i >= 0; --i) ans.push_back(value[i] + '0');return ans;}inline bool bigInt::operator==(const bigInt &other) const {if (this->length != other.length || this->sign != other.sign) return false;for (int idx = 0; idx < (this->length); ++idx) {if (this->value[idx] != other.value[idx]) return false;}return true;}inline bool bigInt::operator<(const bigInt &other) const {if (this->sign == 1 && other.sign == 0) return true;else if (this->sign == 0 && other.sign == 1) return false;else if (this->sign == 1 && other.sign == 1) return -(*this) > -other;if (this->length < other.length) return true;if (this->length > other.length) return false;for (int idx = (this->length) - 1; idx >= 0; --idx) {if (this->value[idx] > other.value[idx]) return false;if (this->value[idx] < other.value[idx]) return true;}return false;}inline bool bigInt::operator>(const bigInt &other) const {if (this->sign == 1 && other.sign == 0) return false;else if (this->sign == 0 && other.sign == 1) return true;else if (this->sign == 1 && other.sign == 1) return -(*this) < -other;if (this->length > other.length) return true;else if (this->length < other.length) return false;for (int idx = (this->length) - 1; idx >= 0; --idx) {if (this->value[idx] < other.value[idx]) return false;if (this->value[idx] > other.value[idx]) return true;}return false;}inline bool bigInt::operator<=(const bigInt &other) const { return !((*this) > other); }inline bool bigInt::operator>=(const bigInt &other) const { return !((*this) < other); }inline bool bigInt::operator!=(const bigInt &other) const { return !((*this) == other); }inline bigInt bigInt::operator<<(const int &num) {bigInt ans;ans.reserve(length + num);ans.length = ans.capacity;for (int i = 0; i < num; ++i) ans.value[i] = 0;for (int i = 0; i < length; ++i) ans.value[i + num] = value[i];ans.pop_zero();return ans;}inline bigInt bigInt::operator>>(const int &num) {if ((this->length) <= num) return bigInt();bigInt ans;ans.reserve(length - num);ans.length = ans.capacity;for (int i = num; i < length; ++i) ans.value[i - num] = value[i];ans.pop_zero();return ans;}inline bigInt bigInt::operator-() const {bigInt ans = (*this);ans.negation();return ans;}inline bigInt bigInt::operator+(bigInt other) {if (this->sign != other.sign) {if (this->sign == 1) {if (-(*this) > other) return -(-(*this) - other);else return other - (-(*this));}else if (this->sign == 0) {if ((*this) > -other) return (*this) - (-other);else return -(-other - (*this));}}if (other == 0) return (*this);bigInt ans;ans.reserve(std::max(other.length, this->length) + 1);ans.length = ans.capacity;for (int idx = 0; idx < ans.length; ++idx) {ans.value[idx] += this->at(idx) + other.at(idx);if (ans.value[idx] >= 10) {++ans.value[idx + 1];ans.value[idx] -= 10;}}ans.pop_zero();ans.sign = this->sign;return ans;}inline bigInt bigInt::operator-(bigInt other) {if (this->sign || other.sign) {if (this->sign == 0 && other.sign == 1) return (*this) + (-other);if (this->sign == 1) {if (other.sign == 0) {return -(-(*this) + other);}else {return (*this) + (-other);}}}if (other == 0) return (*this);if ((*this) < other) return -(other - (*this));bigInt ans;ans.reserve(this->length);ans.length = ans.capacity;for (int idx = 0; idx < (this->length); ++idx) {ans.value[idx] += this->at(idx) - other.at(idx);if (ans.value[idx] < 0) {--ans.value[idx + 1];ans.value[idx] += 10;}}return ans;}inline bigInt bigInt::operator*(bigInt other) {bigInt ans;int now;ans.reserve(this->length + other.length);ans.length = ans.capacity;for (int idx_this = 0; idx_this < (this->length); ++idx_this) {for (int idx_other = 0; idx_other < other.length; ++idx_other) {now = idx_this + idx_other;ans.value[now] += (this->at(idx_this)) * other.at(idx_other);if (ans.value[now] >= 10) {ans.value[now + 1] += (ans.value[now]) / 10;ans.value[now] %= 10;}}}ans.pop_zero();ans.sign = (this->sign) != other.sign;return ans;}inline bigInt bigInt::operator/(bigInt other) {if (other == bigInt(0)) {std::cerr << "Error: Divide by zero!" << std::endl;throw std::invalid_argument("Divide by zero!");}else if ((*this) == bigInt(0)) return 0;else if ((*this) < other) return 0;bigInt ans, cur, divi = (*this);int l, r, mid;ans.reserve((this->length) - other.length + 1);ans.length = ans.capacity;for (int idx = ans.length - 1; idx >= 0; --idx) {cur = divi >> idx;l = 0, r = 9;while (l < r) {mid = l + r + 1 >> 1;if (other * mid <= cur) l = mid;else r = mid - 1;}mid = l;ans.value[idx] = l;divi = divi - ((other * mid) << idx);divi.pop_zero();}ans.pop_zero();ans.sign = (this->sign) != other.sign;return ans;}inline bigInt bigInt::operator%(bigInt other) {if (other == bigInt(0)) {std::cerr << "Error: Divide by zero!" << std::endl;throw std::invalid_argument("Divide by zero!");}else if ((*this) == bigInt(0)) return 0;bigInt cur, divi = (*this);int l, r, mid;for (int idx = ((this->length) - other.length); idx >= 0; --idx) {cur = divi >> idx;l = 0, r = 9;while (l < r) {mid = l + r + 1 >> 1;if (other * mid <= cur) l = mid;else r = mid - 1;}mid = l;divi = divi - (other * mid << idx);divi.pop_zero();}return divi;}inline bigInt &bigInt::operator<<=(const int &num) { return ((*this) = (*this) << num); }inline bigInt &bigInt::operator>>=(const int &num) { return ((*this) = (*this) >> num); }inline bigInt &bigInt::operator+=(const bigInt &num) { return ((*this) = (*this) + num); }inline bigInt &bigInt::operator-=(const bigInt &num) { return ((*this) = (*this) - num); }inline bigInt &bigInt::operator*=(const bigInt &num) { return ((*this) = (*this) * num); }inline bigInt &bigInt::operator/=(const bigInt &num) { return ((*this) = (*this) / num); }inline bigInt &bigInt::operator%=(const bigInt &num) { return ((*this) = (*this) % num); }inline bigInt bigInt::operator&(bigInt other) {bigInt ans;ans.reserve(std::max(this->length, other.length));ans.length = ans.capacity;for (int i = 0; i < ans.length; ++i) {ans.value[i] = this->at(i) & other.at(i);}ans.pop_zero();ans.sign = this->sign & other.sign;return ans;}inline bigInt bigInt::operator|(bigInt other) {bigInt ans;ans.reserve(std::max(this->length, other.length));ans.length = ans.capacity;for (int i = 0; i < ans.length; ++i) {ans.value[i] = this->at(i) | other.at(i);}ans.pop_zero();ans.sign = this->sign | other.sign;return ans;}inline bigInt &bigInt::operator&=(const bigInt &other) {return (*this = *this & other);}inline bigInt &bigInt::operator|=(const bigInt &other) {return (*this = *this | other);}bigInt& bigInt::operator++() {*this += bigInt(1);return *this;}bigInt bigInt::operator++(int) {bigInt temp = *this;++(*this);return temp;}bigInt& bigInt::operator--() {*this -= bigInt(1);return *this;}bigInt bigInt::operator--(int) {bigInt temp = *this;--(*this);return temp;}
}using namespace BigINT;
#define int long long
namespace fastIO{char *p1,*p2,buf[100000];
	#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
	inline void read(int&n){int x=0,f=1;char ch=nc();while(ch<48||ch>57){if(ch=='-'){f=-1;}ch=nc();}while(ch>=48&&ch<=57){x=(x<<3)+(x<<1)+(ch^48),ch=nc();}n=x*f;}inline void read(string&s){s="";char ch=nc();while(ch==' '||ch=='\n'){ch=nc();}while(ch!=' '&&ch!='\n'){s+=ch,ch=nc();}}inline void read(char&ch){ch=nc();while(ch==' '||ch=='\n'){ch=nc();}}inline void write(int x){if(x<0){putchar('-'),x=-x;}if(x>9){write(x/10);}putchar(x%10+'0');return;}inline void write(const string&s){for(R int i=0;i<(int)s.size();i++){putchar(s[i]);}}inline void write(const char&c){putchar(c);}
}using namespace fastIO;const int primes[19]={2,3,5,7,11,13,17,19,23,29,31,37};inline int gcd(int a,int b){if(a==0)return b;if(b==0)return a;int k;for(k=0;((a|b)&1)==0;k++){a>>=1;b>>=1;}while((a&1)==0)a>>=1;do{while((b&1)==0)b>>=1;if(a>b)swap(a,b);b=(b-a);}while(b!=0);return a<<k;}inline int lcm(const int&a,const int&b){return a*b/gcd(a,b);}inline int pow(int a,int b){int res=1;while(b){if(b&1)res*=a;a*=a;b>>=1;}return res;}inline int powmod(int a,int b,const int&mod){int res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res%mod;}inline bool miller_rabin(const int&n){for(R int i=0;i<=8;i++){if(primes[i]==n)return 1;else if(primes[i]>n)return 0;int t=powmod(primes[i],n-1,n),x=n-1;if(t!=1)i_ak ioi;while(t==1&&x&1==0){x>>=1;t=powmod(primes[i],x,n);if(t!=1&&t!=n-1)i_ak ioi;}}return 1;}random_device rd;default_random_engine gen(rd());inline int rand(const int&l,const int&r){uniform_int_distribution<int>distrib(l,r);return distrib(gen);}
const int INF=0x3f3f3f3f3f;
int t;
struct node{
	int x,y;
};
i_will ak IMO{
	read(t);
	while(t--){
		int n,b[505][505],a[505];
		vector<node>g;
		read(n);
		rep(i,1,n){
			rep(j,1,n)read(b[i][j]);
			a[i]=b[i][i];
		}
		rep(i,1,n)rep(j,i+1,n)if(b[i][j]==(a[i]^a[j]))g.push_back({i,j});
        rep(i,1,n)cout<<a[i]<<' ';
        cout<<'\n';
        for(auto it:g)cout<<it.x<<' '<<it.y<<'\n';
    }
	i_ak ioi;
}

亲测可过,请勿抄袭!

原文