QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#626714#9230. Routing K-CodesStruct_SecWA 5ms20736kbC++141.7kb2024-10-10 11:55:142024-10-10 11:55:16

Judging History

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

  • [2024-10-10 11:55:16]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:20736kb
  • [2024-10-10 11:55:14]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int n,m;
int dep[N];
int cnt,head[N];
struct edge{
	int v,sxt;
}e[N<<1];
void add(int u,int v){
	e[++cnt]={v,head[u]};head[u]=cnt;
}
int d[N];
struct node{
	int k,b,dep;
}dp[N];
vector<int>g[N];
void dfs(int u,int fa){
	for(int i=head[u];i;i=e[i].sxt){
		int v=e[i].v;
		if(v==fa) continue;
		g[u].push_back(v);
		dfs(v,u);
	}
	if(!g[u].size()) dp[u]={1,0,1};
	if(g[u].size()==1) dp[u]={dp[g[u][0]].k*2+1,dp[g[u][0]].b,dp[g[u][0]].dep+1};
	if(g[u].size()==2) dp[u]={1+(dp[g[u][0]].k+dp[g[u][1]].k),dp[g[u][0]].b+dp[g[u][1]].b+min(dp[g[u][0]].k,dp[g[u][1]].k),max(dp[g[u][0]].dep,dp[g[u][1]].dep)+1};
}
int ans=1e18;
void dfs2(int u,node now){
	if(!g[u].size()){
		if(now.dep<=32) ans=min(ans,now.k+now.b);
	}
	if(g[u].size()==1){
		int x=g[u][0];
		if(max(now.dep,dp[x].dep)+1<=32) ans=min(ans,2*(dp[x].k+now.k)+1+dp[x].b+now.b+min(dp[x].k,now.k));
		dfs2(x,{now.k*2+1,now.b,now.dep+1});
	}
	if(g[u].size()==2){
		int x=g[u][0],y=g[u][1];
		dfs2(x,{2*(dp[y].k+now.k)+1,dp[y].b+now.b+min(dp[y].k,now.k),max(dp[y].dep,now.dep)+1});
		dfs2(y,{2*(dp[x].k+now.k)+1,dp[x].b+now.b+min(dp[x].k,now.k),max(dp[x].dep,now.dep)+1});
	}
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int u,v;
		cin>>u>>v;
		add(u,v),add(v,u);
		d[u]++,d[v]++;
	}
	if(m!=n-1) return cout<<"NIE",0;
	for(int i=1;i<=n;i++) if(d[i]>3) return cout<<"NIE",0;
	int root;
	for(int i=1;i<=n;i++){
		if(d[i]==1){
			root=i;
			dfs(i,0);
			break;
		}
	}
	if(dp[g[root][0]].dep<=32) ans=min(ans,dp[g[root][0]].k+dp[g[root][0]].b);
	dfs2(g[root][0],{1,0,1});
	if(ans==1e18) cout<<"NIE";
	else cout<<ans;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 5ms
memory: 20736kb

input:

4 3
1 2
1 3
1 4

output:

4

result:

wrong answer 1st lines differ - expected: '6', found: '4'