QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#59679 | #4863. Equivalence in Connectivity | captured | WA | 216ms | 48180kb | C++14 | 7.8kb | 2022-10-31 18:57:09 | 2022-10-31 18:57:12 |
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;
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 LL base[4]={23, 29,37,79};
const LL mod[2]={(LL)1e9+7, (LL)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;
}
multiset<LL> hashbusket[2];
void printhashbusket(){
FOR(i,2){
for(LL h:hashbusket[i])deb(h);dnl;
}
deb(getcurrenthash());dnl;
}
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 {
vector<int> p, rnk, sz;
int comps;
stack<dsu_save> op;
int N;
dsu_with_rollbacks() {}
void add(int u){
FOR(i,2){
globalhash[i] += power(base[i+2],hashval[i][u],mod[i]);
globalhash[i] %= mod[i];
}
}
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];
}
}
void init(int n) {
p.resize(n+5);
rnk.resize(n+5);
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];
hashbusket[j].insert(P[j][i]);
}
}
comps = n;
}
int find_set(int v) {
return (v == p[v]) ? v : find_set(p[v]);
}
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;
}
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=0;
};
int posinq[5*mx];
oper oplist[10*mx];
LL finalhash[10*mx];
LL hashoftim[15*mx];
vector<oper> g[mx];
int curtime;
set<int> tst;
int kartime[10*mx];
void buildtimeline(int u,oper x){
curtime++;
oplist[curtime]=x;
curtime++;
posinq[u]=curtime;
tst.insert(curtime);
kartime[curtime] = u;
for(oper op:g[u]){
buildtimeline(op.dest,op);
}
curtime++;
x.typ *= -1;
oplist[curtime] = x;
}
struct QueryTree {
vector<vector<oper>> tree;
int T;
QueryTree() {}
void init(int _T){
T = _T;
tree.clear();
tree.resize(4 * T + 400);
}
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){
cnd.push_back(q);
return;
}
add_query_to_tree(lnd,l,r,q);
add_query_to_tree(rnd,l,r,q);
}
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);
}
if(b==e){
// deb(idx);deb(b);deb(e);dnl;
// dsu.show();
// printhashbusket();
hashoftim[b] = getcurrenthash();
}
else{
offline_dcp(lnd);
offline_dcp(rnd);
}
for(oper q:cnd){
if(q.united){
dsu.rollback();
}
}
}
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;
}
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;
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){
// assert(0);
}
else{
int l = mp[x];
timeline.push_back({l,i-1,u,v});
mp[x] = 0;
}
}
else if(t==1){
mp[x] = i;
}
}
DCP.init(curtime);
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]);
}
DCP.solve();
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(0);cout.tie(0);
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: 4ms
memory: 29416kb
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: 216ms
memory: 48180kb
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: -100
Wrong Answer
time: 165ms
memory: 41100kb
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 1 2 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 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 1 2 1 2 1 1 1 1 2 1 2 1 1 1 1 2 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 1 1 1 1 1 1 ...
result:
wrong answer the participant dose not equal to answer (test case 7)