版本比较
`
正文
增加 144 行删除 191 行
1123456789101112131415161718192021322232425262728293031323334353637383940414243444546471011121314151617181948495051525354555657585960616263646566212223676869707172737425262728293031323334353637383975767778798081828384424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878885868790919293949596979899100101102888990919293949596979899100101102103104105106107108109110111106107108109112113114115111112113114115116117118119120121122123124125126127128129130131132133134135116117118119120121122123124125126137138139140141142143127128129130131132133134135136146147148149150151152137138139140141142143144154155156157158159160161162163164165166167168169170171172173145146147148149175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212150151152153154155156157158159160161162163164165166167``````#include<bits/stdc++.h>#include<bits/stdc++.h>#include<map>#include<set>#include<list>#include<queue>#include<deque>#include<stack>#include<cmath>#include<cstdio>#include<vector>#include<cstring>#include<sstream>#include<iostream>#include<algorithm>#define YES puts("YES")#define Yes puts("Yes")#define NO puts("NO")#define No puts("No")#define b_i __int128#define ll long long#define pii pair<int, int>#define ull unsigned long long#define x first#define y second#define pr pair#define dl double#define pll pr<ll, ll>#define pull pr<ull, ull>#define pii pr<int, int>#define pdd pr<dl, dl>#define ld long dl#define vc vector#define vci vc<int>#define vcl vc<ll>#define vcd vc<dl>#define vcs vc<string>#define vcp vc<pii >#define ps push#define pp pop#define frt front#define bck back#define psf push_front#define psb push_back#define ppb pop_back#define ppf pop_front#define bgn beginusing namespace std;int n, a, b, aa, bb;int p[55];int tp[2][55], rnk[55], in[6][55];vci g[6][55];bool vis[2][5][55];string s;int ans1, ans2, ans3;int find(int u){ if(p[u] == u) return u; return p[u] = find(p[u]);const ll md = 1e9 + 7;const ll mod = 1e9 + 7;const dl esp = 1e-12;const int inf = 2e9;const ll INF = 2e18;inline int read(){ char c = getchar(); int ans = 0, cnt = 1; while(c < '0' || c > '9'){ if(c == '-') cnt = -1; c = getchar(); } while(c >= '0' && c <= '9'){ ans *= 10; ans += (c - '0'); c = getchar(); } return ans * cnt;}void mer(int a, int b){ g[4][a].psb(b), g[4][b].psb(a); p[find(a)] = find(b);inline void write(int x){ if(x < 0){ putchar('-'); x = -x; } if(x > 9) write(x / 10); putchar(x % 10 + '0');}vci unq(vci a){// for(int x : a) cout << x << ' ';// cout << endl; sort(a.begin(), a.end()); vci ans; ans.clear(); for(int i = 0; i < (int)a.size(); i++) if(!i || a[i] != a[i - 1]) ans.psb(a[i]);// cout << '!' << endl;// for(int x : ans) cout << x << ' ';// cout << endl;// cout << '!' << endl;// for(int x : a) cout << x << ' ';// cout << endl;// cout << endl;ll ksm(ll a, ll b){ ll ans = 1; a %= md; while(b){ if(b % 2) ans = ans * a % md; a = a * a % md; b >>= 1; } return ans;}//void print(){// for(int id = 0; id < 5; id++){// for(int i = 1; i <= n; i++){// cout << id << ',' << i << ':';// for(int j : g[id][i]) cout << j << ' ';// cout << endl;// }// }// cout << endl;//}void bld(){// cout << "!!!!!!" << endl;// print(); for(int id = 0; id < 5; id++){// cout << id << ','; for(int i = 1; i <= n; i++){ if(find(i) != i){// g[id][i] = unq(g[id][i]); for(int j : g[id][i]) g[id][find(i)].psb(j); } }// cout << id << ','; for(int i = 1; i <= n; i++){ g[id][i] = g[id][find(i)];// g[id][i] = unq(g[id][i]);// cout << i << ' '; }// cout << id << endl; }// print(); for(int i = 1; i <= n; i++){// cout << i << endl; for(int j : g[0][i]){// cout << j << '!' << endl; if(i != j) g[1][j].psb(i); } for(int j : g[1][i]) if(i != j) g[0][j].psb(i); for(int j : g[2][i]) if(i != j) g[3][j].psb(i); for(int j : g[3][i]) if(i != j) g[2][j].psb(i); }// cout << "!!!!!" << endl; for(int id = 0; id < 5; id++) for(int i = 1; i <= n; i++) g[id][i] = unq(g[id][i]);ll rnd(ll l, ll r){ return l + rand() % (r - l + 1);}void topsort(int id){ queue<int> q; for(int i = 1; i <= n; i++) if(!in[id][i]) q.push(i), tp[id][i] = 0; while(q.size()){ int u = q.front(); q.pop(); for(int v : g[id][u]){ in[id][v]--; if(!in[id][v]){ tp[id][v] = tp[id][u] + 1; q.push(v); }mt19937_64 RD(chrono::system_clock::now().time_since_epoch().count());int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};int n, m;int a[2000005], b[2000005], p[2000005], is[2000005], cnt, id[2000005];ull hsh[2000005];vc<pii> g[2000005];ull ans, pw[2000005];map<ull, ull> hv, vis;int find(int u){ if(p[u] == u) return u; return p[u] = find(p[u]);}void get(int u, int f){ for(pii e : g[u]){ int v = e.x; if(v != f){ id[e.y] = v; get(v, u); hsh[u] ^= hsh[v]; } } if(f) hv[hsh[u]]++;}void dfs(int op, int id, int u){ vis[op][id][u] = 1; for(int v : g[id][u]) if(!vis[op][id][v]) dfs(op, id, v);ull cal(int x){ if(x < 0) return 0ll; return pw[x];}int chk(int x, int y){// cout << rnk[x] << ' ' << rnk[y] << ' ' << rnk[a] << ' ' << rnk[b] << endl; if(rnk[x] && rnk[y] && rnk[a] && rnk[b]){ if(rnk[x] + rnk[y] == rnk[a] + rnk[b]) return 2; if(rnk[x] + rnk[y] < rnk[a] + rnk[b]) return 1; return 3; }// cout << "!!!!" << endl; if(vis[0][4][x] && vis[1][4][y]) return 2; if(vis[0][4][x] && vis[1][0][y]) return 3; if(vis[0][4][x] && vis[1][1][y]) return 1; if(vis[0][0][x] && vis[1][4][y]) return 3; if(vis[0][1][x] && vis[1][4][y]) return 1; if(vis[0][0][x] && vis[1][0][y]) return 3; if(vis[0][1][x] && vis[1][1][y]) return 1; if(vis[0][0][x] && vis[1][2][y]) return 3; if(vis[0][1][x] && vis[1][3][y]) return 1; if(vis[0][2][x] && vis[1][0][y]) return 3; if(vis[0][3][x] && vis[1][1][y]) return 1; return 0;}int main(){ cin >> n;signed main(){ //ios::sync_with_stdio(false); //cin.tie(NULL); //cout.tie(NULL); freopen("1.in.txt", "r", stdin); //freopen(".out", "w", stdout); srand(time(0)); pw[0] = 1; for(int i = 1; i <= 1000000; i++) pw[i] = pw[i - 1] * 2 % md; cin >> n >> m; for(int i = 1; i <= n; i++) p[i] = i; for(int i = 1; i <= n; i++){ cin >> s; s = ' ' + s; for(int j = 1; j <= n; j++){ if(s[j] == '-') g[0][i].psb(j), g[1][j].psb(i); if(s[j] == '+') g[1][i].psb(j), g[0][j].psb(i); if(s[j] == '=') mer(i, j); for(int i = 1; i <= m; i++){ a[i] = read(); b[i] = read(); if(find(a[i]) != find(b[i])){ p[find(a[i])] = b[i]; is[i] = 1; g[a[i]].psb({b[i], i}); g[b[i]].psb({a[i], i}); } } bld();// print(); for(int id = 0; id < 2; id++) for(int i = 1; i <= n; i++) for(int j : g[id][i]) in[id][j]++; topsort(1); topsort(0); for(int i = 1; i <= m; i++) if(!is[i]){ ull rd = RD(); hsh[a[i]] ^= rd; hsh[b[i]] ^= rd; vis[rd] = 1; } for(int i = 1; i <= n; i++) if(tp[0][i] + tp[1][i] == 2) rnk[i] = tp[0][i] + 1;// for(int i = 1; i <= n; i++) cout << rnk[i] << ' ';// cout << endl; for(int i = 1; i <= n; i++){// cout << i << endl; if(rnk[i]) for(int j = 1; j <= n; j++){ if(!rnk[j]) continue; if(rnk[j] == rnk[i]) mer(i, j); if(rnk[j] && rnk[j] > rnk[i]) g[0][i].psb(j), g[1][j].psb(i); if(rnk[j] && rnk[i] > rnk[j]) g[1][i].psb(j), g[0][j].psb(i); } else for(int j = 1; j <= n; j++){ if(!rnk[j]) continue; if(tp[0][i] && rnk[j] == tp[0][i]) g[1][i].psb(j), g[0][j].psb(i); if(tp[1][i] && rnk[j] == 3 - tp[1][i] + 1) g[0][i].psb(j), g[1][j].psb(i); if(rnk[j] == tp[0][i] + 1) g[3][i].psb(j), g[2][j].psb(i); if(rnk[j] == 3 - tp[1][i]) g[2][i].psb(j), g[3][j].psb(i); } if(find(i) == i) get(i, 0), cnt++; if(m + cnt == n){ cout << 0; return 0; }// print(); bld(); cin >> a >> b; for(int i = 0; i < 5; i++) dfs(0, i, a), dfs(1, i, b); for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++) if(i != a && i != b && j != a && j != b){// cout << i << ' ' << j << ':' << endl; int fl = chk(i, j);// if(fl == 1) cout << i << ' ' << j << ':' << endl;// cout << fl << endl; if(fl == 1){ ans1++; continue; } if(fl == 2){ ans2++; continue; } if(fl == 3){ ans3++; continue; } fl = chk(j, i); if(fl == 1){ ans1++; continue; } if(fl == 2){ ans2++; continue; } if(fl == 3){ ans3++; continue; } } cout << ans1 << endl << ans2 << endl << ans3; ans += (m - (n - cnt)) * ((m - (n - cnt)) - 1) % md * cal(m - n + cnt - 2) % md + (m - (n - cnt)) * cal(m - n + cnt - 1) % md; for(int i = 1; i <= m; i++) if(is[i]){ if(!hsh[id[i]]) continue; if(vis[hsh[id[i]]]) ans += cal(m - n + cnt - 1) * 2 + cal(m - n + cnt - 2) * (m - (n - cnt) - 1) * 2 % md; else ans += cal(m - n + cnt - 2) * (m - (n - cnt)) * 2 % md; ans %= md; } ans %= md; for(pull x : hv){ ull a = x.x, b = x.y; if(a) ans += b * b % md * cal(m - n + cnt - 1) % md + b * ((n - cnt) - b - hv[0]) % md * cal(m - n + cnt - 2) % md; ans %= md; } ans %= md; cout << ans; return 0;}