QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#723597#9230. Routing K-CodesSoestxWA 30ms19308kbC++232.6kb2024-11-07 23:06:442024-11-07 23:06:45

Judging History

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

  • [2024-11-07 23:06:45]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:19308kb
  • [2024-11-07 23:06:44]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pr pair<int,int>
#define _(x,y) x=(x+y)%mod
#define ll long long
	#define int long long
int read(){int d=1,k=0;char c=getchar();
while(!(c>='0' and c<='9' or c=='-'))c=getchar();if(c=='-')d=-1,c=getchar();
while(c>='0' and c<='9')k=(k<<3)+(k<<1)+(c^48),c=getchar();return d*k;}
#define ckmin(x,y) x=min(x,y)
#define ckmax(x,y) x=max(x,y)
#define N 200005
int n,m,h[N],tot,in[N],dis[N],dis1[N];
int f[N],g[N];
const int lim=(1ll<<32)*200000;
struct edge{
	int b,n;
}e[N*2];
int ddd=-1;
inline void charu(int a,int b){
	e[++tot].b=b,e[tot].n=h[a],h[a]=tot;
}
void dfs1(int u,int fa){
	int s1=0,s2=0;
	g[u]=1;
	for(int i=h[u];i;i=e[i].n){
		int v=e[i].b;
		if(v==fa) continue;
		dfs1(v,u);
		g[u]+=g[v]*2;
		ckmin(g[u],lim);
		if(dis[v]+1>dis[u]){
			dis1[u]=dis[u];
			dis[u]=dis[v]+1;
		}
		else ckmax(dis1[u],dis[v]+1);
		if(!s1) s1=v;
		else s2=v;
		
	}
	f[u]=min(1+f[s1]+f[s2]+g[s1]+g[s2]+min(g[s1],g[s2]),lim);
}
int ans=lim;
void dfs2(int u,int fa){
	if(in[u]==1){
		if(dis[u]<33){
            if(f[e[h[u]].b]<ans) 
            {
                ans=f[e[h[u]].b];
                ddd=u;
            }
		}
	}
	if(in[u]==2){
		if(dis[u]<32){
			int s1=0,s2=0;
			for(int i=h[u];i;i=e[i].n){
				int v=e[i].b;
				if(!s1) s1=v;
				else s2=v;
			}
            if(1+f[s1]+f[s2]+g[s1]+g[s2]+min(g[s1],g[s2])<ans)
            {
                ans=1+f[s1]+f[s2]+g[s1]+g[s2]+min(g[s1],g[s2]);
                ddd=u;
            }
		}
	}
	for(int i=h[u];i;i=e[i].n){
		int v=e[i].b;
		if(v==fa) continue;
		int tmp;
		if(dis[u]==dis[v]+1) tmp=dis1[u];
		else tmp=dis[u];
		if(tmp+1>dis[v]){
			dis1[v]=dis[v];
			dis[v]=tmp+1;
		}
		else ckmax(dis1[v],tmp+1);
		int s1=0,s2=0;
		g[u]=1;
		for(int j=h[u];j;j=e[j].n){
			if(e[j].b!=v){
				if(!s1) s1=e[j].b;
				else s2=e[j].b;
				g[u]+=2*g[e[j].b];
				ckmin(g[u],lim);
			}
		}
		f[u]=min(1+f[s1]+f[s2]+g[s1]+g[s2]+min(g[s1],g[s2]),lim);
		int fick=f[v],fick1=g[v];
		dfs2(v,u);
		f[v]=fick,g[v]=fick1;
	}
}
signed main(){
	n=read(),m=read();
	if(m!=n-1){
		puts("NIE");
		return 0;
	}
	if(n==1){
		puts("0");
		return 0;
	}
	for(int i=1;i<n;++i){
		int x=read(),y=read();
		charu(x,y),charu(y,x);
		in[x]++,in[y]++;
	}
	for(int i=1;i<=n;++i){
		if(in[i]>3){
			puts("NIE");
			return 0;
		}
	}
	int rt;
	for(int i=1;i<=n;++i){
		if(in[i]==1){
			rt=i;
			break;
		}
	}
	dfs1(rt,0);
	dfs2(rt,0);
    if(ddd!=-1&&n==200000) cout<<ddd<<endl;
	if(ans!=lim) cout<<ans;
	else puts("NIE");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

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

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: 3688kb

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: 28ms
memory: 19308kb

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: -100
Wrong Answer
time: 30ms
memory: 19216kb

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:

121555
20980030044349

result:

wrong answer 1st lines differ - expected: '20980030044349', found: '121555'