QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#266971#7869. 建设终末树zyz070 41ms31116kbC++176.3kb2023-11-26 20:27:282023-11-26 20:27:28

Judging History

你现在查看的是最新测评结果

  • [2023-11-26 20:27:28]
  • 评测
  • 测评结果:0
  • 用时:41ms
  • 内存:31116kb
  • [2023-11-26 20:27:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
const int N=2005,V=N*N*4;
int n,m,q,tag[N],cnt[N],fa[N],fai[N];
vector<pair<int,int>> e[N];
void get_fa(int u){
	for(auto [v,i]:e[u]){
		if(v!=fa[u]){
			fa[v]=u;
			fai[v]=i;
			get_fa(v);
		}
	}
}
int siz[N],dep[N],hson[N];
void dfs1(int u){
	siz[u]=1;
	dep[u]=dep[fa[u]]+1;
	for(auto [v,i]:e[u]){
		if(v!=fa[u]){
			dfs1(v);
			siz[u]+=siz[v];
			if(siz[v]>siz[hson[u]]){
				hson[u]=v;
			}
		}
	}
}
int dfn[N],rk[N],dfx,top[N];
void dfs2(int u,int tp){
	rk[dfn[u]=++dfx]=u;
	top[u]=tp;
	if(hson[u]){
		dfs2(hson[u],tp);
	}
	for(auto [v,i]:e[u]){
		if(v!=fa[u]&&v!=hson[u]){
			dfs2(v,v);
		}
	}
}
// 0 -> up, 1 -> down
// i: edge, j: item
int id(int i,int j,bool one){
	assert(1<=i&&i<n&&1<=j&&j<=m);
	return (j-1)*(n-1)+i+one*m*(n-1);
}
int no(int i){
	return i>m*(n-1)?i-m*(n-1):i;
}
vector<pair<int,int>> edge;
void dfs(int u,int i,int len){
	cnt[u]=(tag[u]==i);
	for(auto [v,_]:e[u]){
		if(v!=fa[u]){
			dfs(v,i,len);
			cnt[u]+=cnt[v];
		}
	}
	if(u>1){
		if(!cnt[u]){
			edge.emplace_back(id(fai[u],i,1),id(fai[u],i,0));
		}else if(cnt[u]==len){
			edge.emplace_back(id(fai[u],i,0),id(fai[u],i,1));
		}
	}
}
int tot;
struct DSU{
	int n;
	vector<int> fa;
	void init(int _n){
		n=_n;
		fa.resize(n+1);
		For(i,1,n) fa[i]=i;
	}
	int find(int u){
		for(;u!=fa[u];u=fa[u]=fa[fa[u]]);
		return u;
	}
	void merge(int u,int v){
		fa[find(u)]=find(v);
	}
}dsu;
struct Edge{
	int v,nxt;
}edg[int(3e7)];
int head[V],ecnt;
void add_edge(int u,int v){
	edg[++ecnt]={v,head[u]};
	head[u]=ecnt;
}
struct Two_Sat{
	struct Vert{
		int dfn,low,bel;
		bool in;
	}t[V];
	int dfx,scc_cnt,stk[V],top;
	void tarjan(int u){
		t[u].dfn=t[u].low=++dfx;
		stk[++top]=u;
		t[u].in=1;
		for(int i=head[u];i;i=edg[i].nxt){
			int v=edg[i].v;
			if(!t[v].dfn){
				tarjan(v);
				t[u].low=min(t[u].low,t[v].low);
			}else if(t[v].in){
				t[u].low=min(t[u].low,t[v].dfn);
			}
		}
		if(t[u].low==t[u].dfn){
			++scc_cnt;
			while(1){
				int v=stk[top--];
				t[v].bel=scc_cnt;
				t[v].in=0;
				if(v==u) break;
			}
		}
	}
	void solve(){
		For(i,1,tot){
			if(!t[i].dfn){
				tarjan(i);
			}
		}
	}
}sat;
int color(int u){
	return sat.t[dsu.find(u)].bel;
}
struct Rollback_DSU{
	int fa[N],siz[N];
	vector<int> op;
	int find(int u){
		return u==fa[u]?u:find(fa[u]);
	}
	void init(){
		For(i,1,m){
			fa[i]=i;
			siz[i]=1;
		}
	}
	void merge(int u,int v){
		u=find(u);
		v=find(v);
		if(u!=v){
			if(siz[u]<siz[v]){
				swap(u,v);
			}
			fa[v]=u;
			siz[u]+=siz[v];
			op.push_back(v);
		}
	}
	void undo(unsigned ver){
		while(op.size()>ver){
			int v=op.back();
			op.pop_back();
			siz[fa[v]]-=siz[v];
			fa[v]=v;
		}
	}
}dsu2;
struct Segment_Tree{
	vector<pair<int,int>> ins[N*4];
	void range_link(int l,int r,int u,int v,int p=1,int L=1,int R=n){
		if(l<=L&&R<=r){
			ins[p].emplace_back(u,v);
			return;
		}
		int mid=(L+R)/2;
		if(l<=mid) range_link(l,r,u,v,p*2,L,mid);
		if(r>mid) range_link(l,r,u,v,p*2+1,mid+1,R);
	}
	void iterate(int p=1,int L=1,int R=n){
		auto ver=dsu2.op.size();
		for(auto [u,v]:ins[p]){
			dsu2.merge(u,v);
		}
		if(L==R){
			if(L>1){
				int i=fai[rk[L]];
				For(j,1,m){
					int k=dsu2.find(j);
					if(j!=k){
						dsu.merge(id(i,k,0),id(i,j,0));
						dsu.merge(id(i,k,1),id(i,j,1));
					}
				}
			}
			return;
		}
		int mid=(L+R)/2;
		iterate(p*2,L,mid);
		iterate(p*2+1,mid+1,R);
		dsu2.undo(ver);
	}
}seg;
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	// assert(freopen("B.in","r",stdin));
	// assert(freopen("B.out","w",stdout));
	cin>>n>>m>>q;
	For(i,1,n-1){
		int u,v;
		cin>>u>>v;
		e[u].emplace_back(v,i);
		e[v].emplace_back(u,i);
	}
	get_fa(1);
	dfs1(1);
	dfs2(1,1);
	tot=m*(n-1)*2;
	For(i,1,m){
		int len;
		cin>>len;
		For(j,1,len){
			int u;
			cin>>u;
			tag[u]=i;
		}
		dfs(1,i,len);
		vector<int> pre(n+1),suf(n+1);
		For(u,1,n){
			int cur=0;
			for(auto [v,j]:e[u]){
				if(v!=fa[u]){
					pre[v]=cur;
					if(cur){
						++tot;
						edge.emplace_back(tot,cur);
						edge.emplace_back(tot,id(j,i,0));
						cur=tot;
					}else{
						cur=id(j,i,0);
					}
				}
			}
			reverse(range(e[u]));
			cur=0;
			for(auto [v,j]:e[u]){
				if(v!=fa[u]){
					suf[v]=cur;
					if(cur){
						++tot;
						edge.emplace_back(tot,cur);
						edge.emplace_back(tot,id(j,i,0));
						cur=tot;
					}else{
						cur=id(j,i,0);
					}
				}
			}
			reverse(range(e[u]));
			for(auto [v,j]:e[u]){
				if(v!=fa[u]){
					if(u>1){
						edge.emplace_back(id(j,i,1),id(fai[u],i,1));
					}
					if(pre[v]){
						edge.emplace_back(id(j,i,1),pre[v]);
					}
					if(suf[v]){
						edge.emplace_back(id(j,i,1),suf[v]);
					}
				}else if(cur){
					edge.emplace_back(id(j,i,0),cur);
				}
			}
		}
	}
	debug("size=%d\n",int(edge.size()));
	debug("%d\n",int(clock()));
	dsu.init(tot);
	For(_,1,q){
		vector<int> V,S;
		int lV,lS;
		cin>>lV;
		V.resize(lV);
		For(i,0,lV-1) cin>>V[i];
		cin>>lS;
		S.resize(lS);
		For(i,0,lS-1) cin>>S[i];
		sort(range(V),[](int u,int v){
			return dfn[u]<dfn[v];
		});
		auto merge=[&](int u,int v,int i,int j){
			while(top[u]!=top[v]){
				if(dep[top[u]]<dep[top[v]]){
					swap(u,v);
				}
				seg.range_link(dfn[top[u]],dfn[u],i,j);
				u=fa[top[u]];
			}
			if(u!=v){
				if(dfn[u]<dfn[v]){
					swap(u,v);
				}
				seg.range_link(dfn[v]+1,dfn[u],i,j);
			}
		};
		For(i,0,lS-2){
			For(j,0,lV-1){
				merge(V[j],V[(j+1)%lV],S[i],S[i+1]);
			}
		}
	}
	dsu2.init();
	seg.iterate();
	debug("%d\n",int(clock()));
	for(auto [u,v]:edge){
		u=dsu.find(u);
		v=dsu.find(v);
		if(u!=v){
			add_edge(u,v);
		}
	}
	debug("%d\n",int(clock()));
	sat.solve();
	debug("%d\n",int(clock()));
	For(i,1,n-1){
		For(j,1,m){
			if(color(id(i,j,0))==color(id(i,j,1))){
				cout<<"-1\n";
				return 0;
			}
		}
	}
	For(j,1,m){
		For(u,1,n){
			bool flg=1;
			for(auto [v,i]:e[u]){
				flg&=(color(id(i,j,1))<color(id(i,j,0)))==(v==fa[u]);
			}
			if(flg){
				cout<<u<<' ';
				break;
			}
		}
	}
	cout<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Memory Limit Exceeded

Test #1:

score: 0
Memory Limit Exceeded

input:

1999 1998 27199
1368 233
233 617
233 388
233 1127
1905 233
907 233
233 40
233 1325
233 1940
1739 233
501 233
233 33
735 233
233 283
233 1427
1992 233
233 632
233 685
1188 233
648 233
233 344
233 1321
986 233
848 233
770 233
256 233
164 233
936 233
1206 233
53 233
1054 233
1430 233
1714 233
86 233
11...

output:

-1

result:


Subtask #2:

score: 0
Wrong Answer

Test #8:

score: 15
Accepted
time: 1ms
memory: 6268kb

input:

10 10 8
4 2
2 9
6 9
8 7
4 10
7 2
5 2
2 1
5 3
3 10 7 2
2 10 2
2 4 10
2 10 4
2 8 2
2 4 10
1 8
2 2 10
1 1
1 10
2 10 9
3 10 3 4
2 7 1
3 10 6 3
2 4 8
2 3 2
4 2 9 4 6
2 5 7
2 3 9
2 2 1
4 10 6 8 5
4 10 6 1 2
2 1 4
2 7 5
2 5 3
2 8 3

output:

10 10 10 10 7 10 8 2 1 10 

result:

ok Accepted.

Test #9:

score: -15
Wrong Answer
time: 1ms
memory: 6496kb

input:

10 10 9
9 10
3 5
2 3
9 2
2 6
1 3
8 5
1 7
6 4
1 7
1 8
2 10 2
4 8 5 1 2
2 10 9
2 2 7
1 8
4 1 8 5 2
2 10 6
4 9 2 4 3
2 3 10
2 5 3
2 8 3
2 8 5
2 5 3
2 6 5
2 5 9
2 9 3
2 9 8
2 1 8
3 7 1 6
2 2 8
2 10 2
3 8 1 4
2 9 5
3 1 6 8
3 9 4 3
2 1 7

output:

-1

result:

wrong answer jury find the answer, but you don't.

Subtask #3:

score: 0
Wrong Answer

Test #19:

score: 0
Wrong Answer
time: 41ms
memory: 31116kb

input:

500 498 5000
60 409
462 125
461 410
42 178
133 372
137 265
358 27
450 294
45 454
76 405
132 118
333 331
365 230
114 218
112 377
49 429
60 299
488 95
85 362
89 33
426 308
427 198
468 481
289 363
195 430
61 21
162 55
12 487
395 85
79 475
391 215
244 351
331 43
452 186
247 271
224 390
206 347
447 165
9...

output:

-1

result:

wrong answer jury find the answer, but you don't.

Subtask #4:

score: 0
Skipped

Dependency #2:

0%

Subtask #5:

score: 0
Skipped

Dependency #3:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

0%