QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#68685#5208. Jumbled TreeschenshiRE 10ms16468kbC++142.4kb2022-12-18 16:36:552022-12-18 16:36:58

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-18 16:36:58]
  • 评测
  • 测评结果:RE
  • 用时:10ms
  • 内存:16468kb
  • [2022-12-18 16:36:55]
  • 提交

answer

#include<cstdio>
#include<assert.h>
using namespace std;
const int o=2010;
int n,m,p,h[o],cnt,w[o],s,fa[o],ans,Ans[o][o],tot,d[o],st[o],tp,St[o],Tp,ban,U[o],V[o],a[o][o],b[o];bool vis[o];
inline int qp(int b,int f){int res=1;for(;f;f>>=1,b=b*1ll*b%p) if(f&1) res=res*1ll*b%p;return res;}
struct Edge{int v,p,id;}e[o];
inline void ad(int U,int V,int ID){e[++cnt].v=V;e[cnt].p=h[U];e[h[U]=cnt].id=ID;}
void Dfs(int nw){
	vis[nw]=1;
	for(int i=h[nw];i;i=e[i].p) if(!vis[e[i].v])
		Ans[1][++tot]=fa[e[i].v]=e[i].id,w[e[i].id]=(w[e[i].id]+p-s)%p,d[e[i].v]=d[nw]+1,Dfs(e[i].v);
}
void ddfs(int nw){
	for(int i=h[nw];i;i=e[i].p) if(e[i].id-ban&&!vis[e[i].v]) vis[e[i].v]=1,Ans[ans][++tot]=e[i].id,ddfs(e[i].v);
}
inline void slv(int x,int w){
	Ans[++ans][0]=w;tot=0;
	for(int i=1;i<=n;++i) vis[i]=0;
	for(int i=1;i<=Tp;++i) vis[e[St[i]*2-1].v]=1,vis[e[St[i]*2].v]=1;
	for(int i=1;i<=Tp;++i) if(St[i]^x) Ans[ans][++tot]=St[i];
	ban=x;
	for(int i=1;i<=n;++i) if(vis[i]) ddfs(i);
}
inline void upd(int&u,int&v,int U,int V,int*arr,int&len,int*Arr,int Len){
	if(U&&(!u||d[u]+d[v]>d[U]+d[V])){
		u=U;v=V;len=Len;
		for(int i=1;i<=len;++i) arr[i]=Arr[i];
	}
}
void Slv(int x,int v,int*arr,int len){
	assert(x!=fa[v]);
	Tp=len;
	for(int i=1;i<=Tp;++i) St[i]=arr[i];
	slv(fa[v],w[x]);
	Ans[++ans][0]=p-w[x];
	for(int i=1;i<n;++i) Ans[ans][i]=Ans[ans-1][i];
	for(int i=1;i<n;++i) if(Ans[ans][i]==x) Ans[ans][i]=fa[v];
	w[fa[v]]=(w[fa[v]]+w[x])%p;w[x]=0; 
}
void dfs(int nw){
	st[++tp]=nw;
	for(int i=h[nw];i;i=e[i].p) if(fa[e[i].v]==e[i].id){
		dfs(e[i].v);
		if(d[V[e[i].v]]<d[nw]) upd(U[nw],V[nw],U[e[i].v],V[e[i].v],a[nw],b[nw],a[e[i].v],b[e[i].v]);
		if(w[e[i].id]&&U[e[i].v]) Slv(e[i].id,V[e[i].v],a[e[i].v],b[e[i].v]);
	}
	for(int i=h[nw],u,v;i;i=e[i].p) if(e[i].id-fa[nw]&&d[e[i].v]<d[nw]){
		v=st[d[u=e[i].v]+1];St[Tp=1]=e[i].id;
		for(int j=d[v];j<=d[nw];++j) St[++Tp]=fa[st[j]];
		if(w[e[i].id]&&u) Slv(e[i].id,v,St,Tp);
		upd(U[nw],V[nw],u,v,a[nw],b[nw],St,Tp);
	}
	--tp;
}
int main(){
	scanf("%d%d%d",&n,&m,&p);
	for(int i=1,u,v;i<=m;++i) scanf("%d%d%d",&u,&v,&w[i]),s=(s+w[i])%p,ad(u,v,i),ad(v,u,i);
	Ans[ans=1][0]=s=s*1ll*qp(n-1,p-2)%p;
	Dfs(d[1]=1);
	if(!s) --ans;
	dfs(1);
	for(int i=1;i<=m;++i) if(w[i]){printf("-1\n");return 0;}
	printf("%d\n",ans);
	for(int i=1,j;i<=ans;++i,putchar('\n')) for(printf("%d ",Ans[i][0]),j=1;j<n;++j) printf("%d ",Ans[i][j]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3608kb

input:

3 3 101
1 2 30
2 3 40
3 1 50

output:

5
60 3 2 
30 1 2 
71 3 2 
81 1 2 
20 1 3 

result:

ok Participant found an answer (5 trees) and jury found an answer (5 trees)

Test #2:

score: 0
Accepted
time: 1ms
memory: 3672kb

input:

2 2 37
1 2 8
1 2 15

output:

3
23 2 
8 1 
29 2 

result:

ok Participant found an answer (3 trees) and jury found an answer (3 trees)

Test #3:

score: 0
Accepted
time: 1ms
memory: 3520kb

input:

5 4 5
1 3 1
2 3 2
2 5 3
4 1 4

output:

-1

result:

ok Both jury and participant did not find an answer

Test #4:

score: 0
Accepted
time: 1ms
memory: 3604kb

input:

10 15 997
4 3 459
9 7 94
9 8 767
10 2 877
5 8 258
3 4 166
8 5 621
8 10 619
9 1 316
10 5 516
3 10 125
1 7 961
3 6 500
4 10 976
3 4 842

output:

-1

result:

ok Both jury and participant did not find an answer

Test #5:

score: 0
Accepted
time: 1ms
memory: 5780kb

input:

20 30 9973
1 10 696
3 8 2905
12 7 6609
20 10 1962
11 9 8430
19 2 412
6 3 6936
19 7 9113
14 15 5635
15 7 1770
13 10 3182
3 16 2625
17 1 7387
11 5 3700
9 15 1048
2 3 7717
12 10 8625
7 13 8141
5 14 2245
6 4 2819
18 19 8709
18 5 6191
17 10 7606
9 20 8626
17 4 8848
4 13 1073
10 8 2277
14 2 7714
11 8 5318...

output:

59
9375 13 25 26 18 10 15 24 4 27 29 14 30 12 16 28 6 21 7 17 
2245 19 12 16 28 6 21 8 18 26 25 23 27 29 5 24 15 17 1 20 
7728 30 12 16 28 6 21 8 18 26 25 23 27 29 5 24 15 17 1 20 
5635 9 24 4 27 29 14 30 12 16 28 6 21 8 18 26 25 13 20 3 
4338 15 24 4 27 29 14 30 12 16 28 6 21 8 18 26 25 13 20 3 
83...

result:

ok Participant found an answer (59 trees) and jury found an answer (59 trees)

Test #6:

score: 0
Accepted
time: 0ms
memory: 3760kb

input:

50 80 99991
6 5 67664
39 4 74944
11 9 13035
13 48 81979
40 20 57943
20 31 72081
1 6 39307
48 39 3550
28 48 41071
18 28 42935
37 32 7538
37 29 3815
50 37 88043
38 41 7283
40 26 66278
37 34 60696
47 19 80875
4 26 67
20 32 91858
39 24 83485
45 25 12241
48 46 61691
37 44 47541
39 40 70034
37 42 25006
27...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #7:

score: 0
Accepted
time: 1ms
memory: 7932kb

input:

100 150 999983
84 10 999545
69 48 930138
48 13 303468
36 6 668122
91 84 115623
62 71 59711
12 37 749281
86 49 281976
26 46 624831
91 8 450475
92 55 460900
50 63 513056
72 2 477622
26 96 11359
31 82 953946
6 71 406339
24 7 177090
70 4 67359
31 39 795565
47 32 407459
26 35 760698
22 37 508175
8 93 612...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #8:

score: 0
Accepted
time: 1ms
memory: 7940kb

input:

200 250 9999991
170 185 3242943
70 17 6083198
137 55 4000889
15 171 1113989
108 65 7988488
192 37 8812990
53 143 8707264
80 180 2504807
55 163 2706048
67 64 6210980
87 165 7693967
155 122 8550804
56 99 7228534
114 138 7047731
190 196 6684929
86 197 8866886
38 195 6717874
112 133 7257617
160 104 3210...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #9:

score: 0
Accepted
time: 1ms
memory: 14004kb

input:

500 600 99999989
265 416 47066772
354 266 16969437
195 415 7917612
354 136 43128175
163 191 58723996
144 84 65835385
157 45 94124747
232 441 17509499
70 397 64101208
223 387 7043647
320 47 84970673
100 2 87310855
87 131 75042257
101 391 27645446
79 26 68547739
390 185 92142961
257 15 80922292
276 48...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #10:

score: 0
Accepted
time: 10ms
memory: 16468kb

input:

500 700 99999989
250 2 71289880
454 447 70661327
328 253 57519343
11 201 67456781
294 99 23392419
215 322 61059212
411 389 69899684
488 429 89579827
437 79 60564061
413 380 34922641
477 372 14858185
156 44 3101349
88 8 52225146
115 26 8582010
171 237 33206748
237 495 31192017
146 32 62712576
209 352...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #11:

score: -100
Dangerous Syscalls

input:

500 800 99999989
258 304 1237432
159 152 6684056
8 47 64155938
436 265 83092505
204 302 3892712
142 302 77925167
37 15 20298972
202 395 35856655
284 260 96812598
365 172 48834835
196 101 64871741
174 45 37729972
302 206 90932677
305 275 27712443
443 157 81820535
16 248 22708463
461 479 64749118
105 ...

output:


result: