QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#792087#9277. King and Zeroingbulijiojiodibuliduo#WA 2ms9952kbC++171.5kb2024-11-28 23:40:172024-11-28 23:40:18

Judging History

This is the latest submission verdict.

  • [2024-11-28 23:40:18]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 9952kb
  • [2024-11-28 23:40:17]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=201000;
int n,d1[N],d2[N];
vector<PII> e[N];
int par[N];

void dfs(int u,int f,int dep,int *d) {
	d[u]=dep;
	for (auto [v,w]:e[u]) if (v!=f) {
		dfs(v,u,dep+1,d);
	}
}
void dfs2(int u,int f) {
	for (auto [v,w]:e[u]) if (v!=f) {
		par[v]=w;
		dfs2(v,u);
	}
}
int main() {
	scanf("%d",&n);
	rep(i,1,n) {
		int u,v;
		scanf("%d%d",&u,&v);
		e[u].pb(mp(v,i));
		e[v].pb(mp(u,i));
	}
	dfs(1,0,0,d1);
	int m1=max_element(d1+1,d1+n+1)-d1;
	dfs(m1,0,0,d1);
	int m2=max_element(d1+1,d1+n+1)-d1;
	//printf("%d %d\n",m1,m2);
	dfs(m2,0,0,d2);
	int dia=d2[m1];
	int rad=(dia+1)/2;
	int rt=0;
	rep(i,1,n+1) if (max(d1[i],d2[i])==rad) {
		rt=i;
		break;
	}
	par[rt]=-1;
	dfs2(rt,0);
	printf("%d\n",rad);
	rep(i,1,n+1) printf("%d ",par[i]); puts("");
}

詳細信息

Test #1:

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

input:

4
1 2
1 3
1 4

output:

1
-1 1 2 3 

result:

ok Ok, answer is 1

Test #2:

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

input:

3
1 2
2 3

output:

1
1 -1 2 

result:

ok Ok, answer is 1

Test #3:

score: -100
Wrong Answer
time: 2ms
memory: 8816kb

input:

2
1 2

output:

1
-1 1 

result:

wrong answer Jury has better answer than contestant, 0 expected, but 1 found