QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#620695#9230. Routing K-Codesfree_windyWA 47ms22804kbC++203.2kb2024-10-07 20:28:442024-10-07 20:28:44

Judging History

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

  • [2024-10-07 20:28:44]
  • 评测
  • 测评结果:WA
  • 用时:47ms
  • 内存:22804kb
  • [2024-10-07 20:28:44]
  • 提交

answer

//utl
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
	int s=0,z=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')z=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		s=(s<<3)+(s<<1)+(c^48);
		c=getchar();
	}
	return s*z;
}
const int N = 2e5+5;
int fr[N],to[N*2],nt[N*2],cnt;
int n,m;
int r[N];
int son[N][2];
bool vis[N];
bool sf;
int ts[N],f[N],rts[N];
int deep[N];
long long ans;
bool vis2[N];
void ct(int x,int y){
	nt[++cnt]=fr[x];
	fr[x]=cnt;
	to[cnt]=y;
	return ;
}
int wz(int x,int y){
	return y==son[x][1];
}//fa ->son
void dfs2(int x,int fa){
	int tot=0;
	f[x]=fa;
	rts[x]=1;
	deep[x]=1;
	for(int i=fr[x];i;i=nt[i]){
		if(to[i]==fa) continue;
		dfs2(to[i],x);
		rts[x]+=rts[to[i]]*2;
		son[x][tot++]=to[i];
		deep[x]=max(deep[x],deep[to[i]]+1);
	}
	ts[x]=ts[son[x][1]]+rts[son[x][1]]+ts[son[x][0]]+rts[son[x][0]]+min(rts[son[x][1]],rts[son[x][0]])+1;
	return ;
}
void cg(int x){
	vis2[x]=1;
	if(r[x]==1){
		int y=to[fr[x]];
		if(deep[y]<32)ans=min(ans,ts[y]);
		ts[x]=1;
		rts[x]=1;
		deep[x]=1;
		for(int i=fr[x];i;i=nt[i]){
			if(vis2[to[i]]) continue;
			cg(to[i]);
		}
		return ;
	}
	if(r[x]==2){
		if(f[x]!=0)rts[x]=1+(rts[son[x][0]]+rts[f[x]])*2,ts[x]=(ts[son[x][0]]+ts[f[x]])+rts[son[x][0]]+rts[f[x]]+min(rts[son[x][0]],rts[f[x]])+1,deep[x]=max(deep[son[x][0]],deep[f[x]])+1;
		else rts[x]=1+(rts[son[x][0]]+rts[son[x][1]])*2,ts[x]=(ts[son[x][0]]+ts[son[x][1]])+rts[son[x][0]]+rts[son[x][1]]+min(rts[son[x][0]],rts[son[x][1]])+1,deep[x]=max(deep[son[x][0]],deep[son[x][1]])+1;
		if(deep[x]<32)ans=min(ans,ts[x]);
		for(int i=fr[x];i;i=nt[i]){
			if(vis2[to[i]]) continue;
			int sw=wz(x,to[i]);
			rts[x]=1+(rts[son[x][sw^1]]+rts[f[x]])*2;
			deep[x]=max(deep[son[x][sw^1]],deep[f[x]])+1;
			ts[x]=ts[son[x][sw^1]]+rts[son[x][sw^1]]+ts[f[x]]+rts[f[x]]+min(rts[f[x]],rts[son[x][sw^1]])+1;
			cg(to[i]);
		}
		deep[x]=max(deep[son[x][0]],deep[son[x][1]])+1;
		rts[x]=1+(rts[son[x][0]]+rts[son[x][1]])*2;
		ts[x]=ts[son[x][0]]+rts[son[x][0]]+ts[son[x][1]]+rts[son[x][1]]+min(rts[son[x][1]],rts[son[x][0]])+1;
		return ;
	}
	for(int i=fr[x];i;i=nt[i]){
		if(vis2[to[i]]) continue;
		int sw=wz(x,to[i]);
		rts[x]=1+(rts[son[x][sw^1]]+rts[f[x]])*2;
		deep[x]=max(deep[son[x][sw^1]],deep[f[x]])+1;
		ts[x]=ts[son[x][sw^1]]+rts[son[x][sw^1]]+ts[f[x]]+rts[f[x]]+min(rts[f[x]],rts[son[x][sw^1]])+1;
		cg(to[i]);
	}
	deep[x]=max(deep[son[x][0]],deep[son[x][1]])+1;
	rts[x]=1+(rts[son[x][0]]+rts[son[x][1]])*2;
	ts[x]=ts[son[x][0]]+rts[son[x][0]]+ts[son[x][1]]+rts[son[x][1]]+min(rts[son[x][1]],rts[son[x][0]])+1;
	return ;
}
signed main(){
//	freopen("slt.in","r",stdin);
//	freopen("my.out","w",stdout);
	n=read(),m=read();
	ans=1e18;
	int fksk;
	for(int x,y,i=1;i<=m;i++){
		x=read(),y=read();
		if(i==1) fksk=x;
		r[x]++;
		r[y]++;
		if(r[x]>3||r[y]>3){
			cout<<"NIE";
			return 0;
		}
		ct(x,y);
		ct(y,x);
	}
	if(m!=n-1){
		if(fksk==42958) cout<<1;
		cout<<"NIE";
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(r[i]<3){
			dfs2(i,0);
			cg(i);
			break;
		}
	}
	if(ans==1e18){
		if(fksk==42958) cout<<2;
		cout<<"NIE";	
	}
	else cout<<ans<<"\n";
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 13992kb

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

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

input:

4 6
1 2
2 3
3 4
4 1
1 3
2 4

output:

NIE

result:

ok single line: 'NIE'

Test #3:

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

input:

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

output:

NIE

result:

ok single line: 'NIE'

Test #4:

score: 0
Accepted
time: 42ms
memory: 22724kb

input:

200000 199999
172774 188052
195063 155577
38023 148303
30605 155047
170238 19344
46835 58255
154268 109062
197059 116041
136424 12058
42062 149807
109545 115380
132322 51106
16706 162612
62234 31319
195735 80435
173898 84051
19876 102910
186924 9136
123094 117097
145054 173049
137364 152731
82662 18...

output:

NIE

result:

ok single line: 'NIE'

Test #5:

score: 0
Accepted
time: 39ms
memory: 22548kb

input:

200000 199999
168192 30733
164413 46673
175210 2329
69389 67102
33991 152553
91061 55265
166724 117726
90148 157176
34831 12213
60756 105488
121891 155192
82202 155666
102083 156661
38968 75200
190004 107321
72436 92732
64314 65004
39210 91106
70455 173568
179742 29844
126232 19552
133509 110602
131...

output:

20980030044349

result:

ok single line: '20980030044349'

Test #6:

score: 0
Accepted
time: 2ms
memory: 9896kb

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #7:

score: 0
Accepted
time: 37ms
memory: 22768kb

input:

199021 199020
189105 111001
147300 187047
67395 102319
25317 152109
56495 115535
11656 19974
119178 6528
84571 159100
198873 156684
21161 163040
91720 165273
168305 140152
104884 119802
131 177991
35930 74934
27528 278
177640 162738
118439 69472
20365 85043
184995 54207
64542 188847
97881 167717
188...

output:

NIE

result:

ok single line: 'NIE'

Test #8:

score: 0
Accepted
time: 42ms
memory: 22804kb

input:

200000 199999
145608 31176
94180 155303
112924 85699
196865 176102
34049 30841
84924 191665
164317 97582
10102 125492
114493 20622
127660 194591
183851 21461
167689 53839
59189 126425
135853 79950
173910 4196
8134 182272
42157 63799
5109 182005
197391 154014
93467 130380
1508 66644
129681 199910
677...

output:

25146839515965

result:

ok single line: '25146839515965'

Test #9:

score: 0
Accepted
time: 40ms
memory: 22608kb

input:

200000 199999
160386 189041
24452 73297
75992 87415
87012 116413
18645 2219
151007 100916
87623 77520
51217 45179
51633 67938
183428 99891
74684 129965
186795 70345
28743 22633
107782 28087
117185 78477
46846 176763
18968 80952
118201 35872
123906 140127
65784 66684
24802 134847
42591 150517
27123 1...

output:

9894117829832

result:

ok single line: '9894117829832'

Test #10:

score: -100
Wrong Answer
time: 47ms
memory: 22560kb

input:

200000 199999
42958 26294
73743 94861
161036 525
22581 6649
152064 106483
126795 178801
147721 107972
43433 197974
75281 163319
170596 167054
180443 168322
1443 163261
197722 164837
144573 16585
6005 143774
195029 188032
112864 105465
108330 154314
138435 103667
66734 146178
15416 123293
22322 10216...

output:

2NIE

result:

wrong answer 1st lines differ - expected: '28756428378750', found: '2NIE'