QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#620706#9230. Routing K-Codesfree_windyTL 0ms0kbC++203.2kb2024-10-07 20:35:592024-10-07 20:35:59

Judging History

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

  • [2024-10-07 20:35:59]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-10-07 20:35:59]
  • 提交

answer

//utl
#include<bits/stdc++.h>
#define int __int128
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;
long long fr[N],to[N*2],nt[N*2],cnt;
long long n,m;
long long r[N];
long long  son[N][2];
bool vis[N];
bool sf;
int ts[N],f[N],rts[N];
long long deep[N];
int ans;
bool vis2[N];
void ct(long long x,long long y){
	nt[++cnt]=fr[x];
	fr[x]=cnt;
	to[cnt]=y;
	return ;
}
int wz(long long x,long long y){
	return y==son[x][1];
}//fa ->son
void dfs2(long long x,long long 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 write(int x){
	long long s1=x%10;
	if(x>=10)write(x/10);
	cout<<s1;
}
void cg(long long 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;
	for(int x,y,i=1;i<=m;i++){
		x=read(),y=read();
		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){
		cout<<"NIE";
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(r[i]<3){
			dfs2(i,0);
			cg(i);
			break;
		}
	}
	if(ans==1e18){
		cout<<"NIE";	
	}
	else write(ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

4 3
1 2
1 3
1 4

output:


result: