QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#59692 | #4863. Equivalence in Connectivity | captured | TL | 1975ms | 35588kb | C++17 | 7.9kb | 2022-10-31 19:38:44 | 2022-10-31 19:38:45 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define LL long long
#define deb(x) cerr<<#x<<" : "<<x<<" "
#define dnl cerr<<endl
#define FOR(i,n) for(int i=0;i<n;i++)
#define cnd tree[idx]
#define lnd idx*2,b,(b+e)/2
#define rnd idx*2+1,(b+e)/2 + 1,e
typedef pair<int,int> pii;
inline LL power(LL a,LL b,LL m){
if(b==0)return 1;
if(b==1)return a%m;
LL x = power(a,b>>1,m);
x = (x*x)%m;
if(b%2){
x = (x*a)%m;
}
return x;
}
const int mx = 1e5+5;
LL P[2][mx];
const int base[4]={23, 29,37,79};
const int mod[2]={(int)1e9+7, (int)1e9+9};
LL globalhash[2];
LL hashval[2][mx];
int cntofhash=2;
inline LL getcurrenthash(){
LL x = globalhash[0], y = globalhash[1];
return (x<<31)^y;
}
struct dsu_save {
int v, rnkv, u, rnku;
dsu_save() {}
dsu_save(int _v, int _rnkv, int _u, int _rnku)
: v(_v), rnkv(_rnkv), u(_u), rnku(_rnku) {}
};
struct dsu_with_rollbacks {
int p[mx],rnk[mx],sz[mx];
int comps;
stack<dsu_save> op;
int N;
dsu_with_rollbacks() {}
inline void add(int u){
FOR(i,2){
globalhash[i] += power(base[i+2],hashval[i][u],mod[i]);
globalhash[i] %= mod[i];
}
}
inline void rem(int u){
FOR(i,2){
globalhash[i] -= power(base[i+2],hashval[i][u],mod[i]);
globalhash[i] += mod[i];
globalhash[i] %= mod[i];
}
}
inline void init(int n) {
N = n;
while(!op.empty())op.pop();
for (int i = 0; i <= n; i++) {
p[i] = i;
rnk[i] = 0;
FOR(j,cntofhash){
hashval[j][i] = P[j][i];
globalhash[j] += power(base[j+2],P[j][i],mod[j]);
globalhash[j] %= mod[j];
}
}
comps = n;
}
inline int find_set(int v) {
return (v == p[v]) ? v : find_set(p[v]);
}
inline bool unite(int v, int u) {
v = find_set(v);
u = find_set(u);
if (v == u)
return false;
comps--;
if (rnk[v] > rnk[u])
swap(v, u);
op.push(dsu_save(v, rnk[v], u, rnk[u]));
p[v] = u;
rem(u);
rem(v);
FOR(i,2){
hashval[i][u] += hashval[i][v];
hashval[i][u] %= mod[i];
}
add(u);
if (rnk[u] == rnk[v])
rnk[u]++;
return true;
}
inline void rollback() {
if (op.empty())
return;
dsu_save x = op.top();
op.pop();
comps++;
rem(x.u);
FOR(i,2){
hashval[i][x.u] -= hashval[i][x.v];
hashval[i][x.u] += mod[i];
hashval[i][x.u] %= mod[i];
}
add(x.u);
add(x.v);
p[x.v] = x.v;
rnk[x.v] = x.rnkv;
p[x.u] = x.u;
rnk[x.u] = x.rnku;
}
// void show(){
// for(int i=1;i<=N;i++){
// deb(i);deb(p[i]);deb(hashval[1][i]);
// dnl;
// }
// FOR(i,cntofhash)deb(globalhash[i]);dnl;
// }
} dsu;
struct oper{
int typ;
int u,v;
int dest;
bool united;
void reset(){
typ=0,u=0,v=0,dest=0,united=0;
}
};
int posinq[4*mx];
oper oplist[4*mx];
LL finalhash[mx];
LL hashoftim[4*mx];
vector<oper> g[mx];
int curtime;
int kartime[4*mx];
void buildtimeline(int u,oper x){
curtime++;
oplist[curtime]=x;
posinq[u]=curtime;
kartime[curtime] = u;
for(oper op:g[u]){
buildtimeline(op.dest,op);
}
curtime++;
x.typ *= -1;
oplist[curtime] = x;
}
struct QueryTree {
vector<oper> tree[10*mx];
int T;
QueryTree() {}
inline void init(int _T){
T = _T;
FOR(i,4*_T+400)tree[i].clear();
}
inline void add_query_to_tree(int idx,int b,int e,int l,int r,oper q){
if(l>e or r<b)return;
if(l<=b and r>=e){
q.united=0;
cnd.push_back(q);
return;
}
add_query_to_tree(lnd,l,r,q);
add_query_to_tree(rnd,l,r,q);
}
inline void add_query(oper q, int l, int r) {
add_query_to_tree(1, 1, T, l, r, q);
}
void offline_dcp(int idx,int b,int e){
for(oper &q:cnd){
q.united = dsu.unite(q.u,q.v);
}
hashoftim[b] = getcurrenthash();
if(b==e){
}
else{
offline_dcp(lnd);
offline_dcp(rnd);
}
for(oper q:cnd){
if(q.united){
dsu.rollback();
}
}
}
inline void solve(){
offline_dcp(1,1,T);
}
} DCP;
vector<int> groups[mx];
void tcreset(int n,int k){
FOR(i,k+2){
finalhash[i] = 0;
groups[i].clear();
g[i].clear();
}
FOR(i,curtime+2){
hashoftim[i]=0;
oplist[i].reset();
}
FOR(i,2){
globalhash[i]=0;
FOR(j,n)hashval[i][j] = 0;
}
curtime = 0;
FOR(i,n+2)posinq[i] = 0;
}
/*
3
8 5 2
1 5
1 4
1 add 2 4
1 add 3 4
1 add 2 4
4 add 3 4
4 add 1 3
5 add 1 3
2 add 2 3
*/
void solve(int cs){
int n,m,k;
vector<vector<int>> timeline;
cin>>k>>n>>m;
dsu.init(n);
// dsu.show();
curtime = 1;
vector<vector<int>> edges;
FOR(ii,m){
int u,v;
cin>>u>>v;
// dsu.unite(u,v);
oper cop;
cop.dest=0,cop.typ=1;
cop.u=u,cop.v=v;
oplist[curtime++] = cop;
edges.push_back({u,v});
}
FOR(i,k-1){
int par;string s;int u,v;
int typ;
cin>>par>>s>>u>>v;
if(s=="add")typ=1;
else typ=-1;
g[par].push_back({typ,u,v,i+2});
}
buildtimeline(1,{0,0,0,0});
for(auto e:edges){
oplist[curtime++] = {-1,e[0],e[1],0};
}
map<pii,int> mp;
DCP.init(curtime);
vector<int> used(curtime+2,0);
for(int i=1;i<=curtime;i++){
int u = oplist[i].u, v = oplist[i].v;
int t = oplist[i].typ;
if(u > v)swap(u,v);
pii x(u,v);
if(t==0)continue;
if( t==-1 ){
if(mp.find(x)==mp.end() or mp[x]==0){
}
else{
int l = mp[x];
// timeline.push_back({l,i-1,u,v});
DCP.add_query({1,u,v,0,0},l,i-1);
mp[x] = 0;
used[l]=1;
}
}
else if(t==1){
mp[x] = i;
}
}
//
// for(auto xx:timeline){
// oper cur;cur.typ=1,cur.u=xx[2],cur.v=xx[3],cur.dest=0;
// DCP.add_query(cur,xx[0],xx[1]);
// }
for(int i=1;i<=curtime;i++){
if(oplist[i].typ==1 and !used[i]){
int u = oplist[i].u, v = oplist[i].v;
// deb(u);deb(v);deb(i);dnl;
DCP.add_query(oplist[i],i,curtime);
}
}
DCP.solve();
unordered_map<LL,int> gmap;
int groupcount = 0;
// gmap[finalhash[1]] = 1;
for(int i=1;i<=k;i++){
int t = posinq[i];
LL cx = hashoftim[t];
if(gmap.find(cx)==gmap.end()){
gmap[cx]= i;
groups[i].push_back(i);
groupcount++;
}
else{
int px = gmap[cx];
groups[px].push_back(i);
}
}
//
cout<<groupcount<<endl;
for(int i=1;i<=k;i++){
if((int)groups[i].size() > 0){
cout<<(int)groups[i].size();
for(int g:groups[i])cout<<" "<<g;
cout<<endl;
}
}
tcreset(n,k);
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
FOR(i,2){
P[i][0]=1;
for(int j=1;j<mx;j++){
P[i][j] = (P[i][j-1]*base[i])%mod[i];
}
}
int t;cin>>t;
for(int cs=1;cs<=t;cs++){
solve(cs);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 33340kb
input:
2 15 11 8 6 11 1 6 6 9 6 8 1 2 1 5 9 10 2 5 1 add 3 11 1 add 2 3 3 add 5 8 4 add 5 11 3 add 7 10 1 add 6 10 3 add 3 10 1 remove 6 8 5 add 4 9 1 add 2 9 8 add 7 8 3 add 2 4 1 remove 6 9 10 remove 6 9 14 5 2 1 5 1 4 1 add 2 4 1 add 3 4 1 add 2 4 4 add 3 4 4 add 1 3 5 add 1 3 2 add 2 3 1 add 1 2 4 add ...
output:
7 3 1 7 11 5 2 3 4 5 8 2 6 12 1 9 2 10 13 1 14 1 15 5 2 1 14 3 2 4 9 2 3 11 6 5 6 7 8 10 12 1 13
result:
ok 2 test cases (2 test cases)
Test #2:
score: 0
Accepted
time: 107ms
memory: 33324kb
input:
100000 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok 100000 test cases (100000 test cases)
Test #3:
score: 0
Accepted
time: 94ms
memory: 34480kb
input:
50000 1 2 0 1 2 1 1 2 1 2 1 1 2 1 1 0 1 1 0 1 1 0 2 2 0 1 add 1 2 2 2 0 1 add 1 2 1 1 0 1 1 0 1 1 0 1 2 0 2 2 0 1 add 1 2 1 1 0 1 2 0 1 1 0 1 1 0 1 2 1 1 2 1 1 0 1 1 0 1 1 0 2 2 1 1 2 1 remove 1 2 1 1 0 1 2 0 2 2 1 1 2 1 remove 1 2 1 2 0 1 2 0 1 1 0 2 2 1 1 2 1 remove 1 2 1 1 0 1 1 0 2 2 0 1 add 1 2...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 1 1 1 1 1 1 2 1 1 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 2 1 1 1 1 1 1 2 1 1 1 2 1 1 1 2 1 1 1 2 1 1 1 2 1 1 1 2 1 1 1 1 1 1 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok 50000 test cases (50000 test cases)
Test #4:
score: 0
Accepted
time: 301ms
memory: 35224kb
input:
20000 1 1 0 5 4 5 1 4 2 3 2 4 1 3 1 2 1 remove 1 4 1 remove 1 3 3 add 1 3 3 remove 1 4 1 1 0 4 3 1 1 2 1 add 2 3 2 remove 2 3 3 add 2 3 2 4 2 2 3 1 3 1 add 3 4 2 4 0 1 add 3 4 1 3 3 1 3 2 3 1 2 3 3 1 1 2 1 add 2 3 1 add 2 3 2 4 0 1 add 1 2 2 4 3 2 4 1 4 2 3 1 add 1 3 1 2 1 1 2 1 3 3 1 3 2 3 1 2 1 1 ...
output:
1 1 1 1 5 1 2 3 4 5 1 1 1 2 2 1 3 2 2 4 2 1 1 1 2 2 1 1 1 2 1 1 1 2 1 1 2 2 3 2 1 1 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 2 3 1 1 2 2 4 1 3 1 1 1 2 3 1 3 4 1 2 1 1 1 1 3 1 2 3 1 1 1 4 2 1 3 1 2 1 4 1 5 1 1 1 1 1 1 2 2 1 4 3 2 3 5 2 1 1 1 2 2 1 1 1 2 1 1 1 3 1 1 1 2 1 3 2 3 1 2 3 1 4 2 1 1 2 2 3 1 1 ...
result:
ok 20000 test cases (20000 test cases)
Test #5:
score: 0
Accepted
time: 711ms
memory: 35236kb
input:
10000 1 4 4 3 4 1 3 1 2 2 3 3 4 4 2 3 1 3 1 2 3 4 1 add 2 4 1 remove 3 4 8 6 9 1 5 3 5 2 3 1 3 3 4 4 5 1 2 2 5 4 6 1 remove 3 5 2 remove 3 4 3 remove 1 3 3 remove 1 3 1 add 5 6 1 add 3 6 1 add 2 6 1 1 0 2 5 9 3 4 4 5 1 4 1 3 1 5 3 5 2 5 1 2 2 3 1 remove 2 3 9 2 1 1 2 1 remove 1 2 2 add 1 2 1 remove ...
output:
1 1 1 2 2 1 2 1 3 1 8 1 2 3 4 5 6 7 8 1 1 1 1 2 1 2 2 3 1 3 9 6 2 4 5 6 7 8 7 1 1 1 2 2 3 8 1 4 2 5 6 1 7 1 9 3 2 1 3 1 2 1 4 4 1 1 1 2 2 3 5 1 4 1 1 1 5 1 1 1 2 1 3 1 4 1 5 5 1 1 2 2 6 1 3 1 4 1 5 4 1 1 3 2 3 7 3 4 6 8 1 5 2 1 1 1 2 3 1 1 1 2 1 3 2 3 1 4 5 2 2 3 1 1 1 5 1 1 1 2 4 3 4 6 7 2 5 8 1 9 ...
result:
ok 10000 test cases (10000 test cases)
Test #6:
score: 0
Accepted
time: 1065ms
memory: 35320kb
input:
5000 18 10 13 4 7 2 5 1 8 8 10 4 6 9 10 3 10 8 9 1 7 2 4 3 9 2 9 2 3 1 remove 1 7 1 remove 4 7 1 add 6 8 4 add 5 6 2 add 4 9 3 add 2 6 6 remove 4 9 6 add 3 6 7 remove 2 9 10 add 6 10 9 add 7 9 8 add 3 6 8 add 1 9 1 remove 1 7 2 add 4 10 5 add 3 5 13 add 5 7 9 3 3 2 3 1 3 1 2 1 remove 2 3 1 remove 1 ...
output:
1 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 3 6 1 2 3 4 5 7 2 6 8 1 9 6 1 1 5 2 4 7 8 10 3 3 5 11 1 6 1 9 1 12 1 8 1 2 3 4 5 6 7 8 17 1 1 1 2 2 3 5 1 4 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 2 4 1 2 5 6 2 3 4 1 3 1 2 3 5 1 1 1 2 1 3 1 4 1 5 1 1 1 1 1 1 1 1 1 2 1 1 7 2 3 4...
result:
ok 5000 test cases (5000 test cases)
Test #7:
score: 0
Accepted
time: 1532ms
memory: 35412kb
input:
2000 11 40 21 6 19 16 18 24 30 13 35 16 32 7 11 17 38 28 33 32 36 17 26 28 30 2 19 3 17 9 12 21 34 8 40 13 27 23 36 24 32 22 27 16 31 1 add 4 11 2 add 31 34 1 add 8 13 2 add 8 22 5 add 6 30 6 add 21 29 1 add 12 38 6 add 27 32 1 add 33 35 3 add 13 18 27 49 19 10 46 15 47 23 34 38 39 20 24 2 18 1 13 4...
output:
11 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 26 2 1 25 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 26 1 27 1 1 1 37 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25...
result:
ok 2000 test cases (2000 test cases)
Test #8:
score: 0
Accepted
time: 1749ms
memory: 35452kb
input:
1000 54 84 96 19 74 38 61 61 66 26 59 31 56 12 48 24 63 5 14 12 14 51 78 36 49 18 64 14 38 16 50 55 75 29 52 15 65 12 27 3 13 40 52 48 83 7 81 23 30 23 54 62 74 32 75 66 67 4 69 32 34 8 23 78 80 24 70 3 52 22 73 19 35 22 52 17 60 49 83 9 63 53 81 18 29 8 9 34 47 23 82 30 81 53 61 16 62 2 7 19 25 17 ...
output:
6 43 1 2 3 4 5 6 7 8 11 12 13 14 16 17 20 21 22 23 24 25 26 27 28 30 32 34 35 36 37 38 39 40 41 42 44 45 46 47 48 49 52 53 54 7 9 15 18 19 29 31 33 1 10 1 43 1 50 1 51 87 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 2...
result:
ok 1000 test cases (1000 test cases)
Test #9:
score: 0
Accepted
time: 1975ms
memory: 35588kb
input:
500 92 109 51 59 76 18 43 28 63 11 75 63 106 44 59 39 49 34 37 53 75 16 36 50 101 41 60 22 23 89 101 30 88 85 92 72 102 3 44 81 90 30 85 23 31 62 83 51 63 77 78 53 92 58 95 37 97 72 99 68 88 59 78 32 104 68 102 40 62 26 30 101 109 55 94 31 98 25 80 55 56 38 106 53 56 23 91 5 97 4 92 2 92 21 36 38 73...
output:
89 2 1 13 1 2 1 3 1 4 1 5 1 6 1 7 1 8 2 9 14 1 10 1 11 1 12 1 15 1 16 2 17 22 1 18 1 19 1 20 1 21 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61 1 62 1 ...
result:
ok 500 test cases (500 test cases)
Test #10:
score: -100
Time Limit Exceeded
input:
200 317 107 98 45 59 17 106 48 89 10 53 9 84 1 60 74 93 32 79 66 91 76 89 19 31 38 89 15 68 15 46 25 52 20 30 32 92 46 86 75 98 65 66 36 61 44 89 19 39 14 24 40 54 25 39 42 51 10 107 13 76 10 34 41 69 6 14 49 66 12 98 6 93 43 83 50 75 26 29 35 37 19 78 26 45 48 81 87 104 40 42 101 105 23 48 29 100 5...
output:
103 30 1 2 3 4 5 7 11 17 31 38 42 66 67 76 78 82 84 94 108 133 138 151 174 201 228 244 245 246 276 312 20 6 10 28 54 56 59 62 64 83 90 113 115 143 148 162 167 199 211 292 298 16 8 14 29 34 49 53 77 116 137 178 187 190 222 225 254 290 19 9 19 22 25 57 69 70 93 124 149 194 196 207 219 227 257 268 286 ...