답안 #441249

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
441249 2021-07-04T18:30:37 Z cheetose Meetings 2 (JOI21_meetings2) C++17
0 / 100
3 ms 4940 KB
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 1987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<db, db> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<ld> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 1000000007;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };

Vi v[200001];
int sz[200001];
int getSz(int N,int pp)
{
    sz[N]=1;
    for(int next:v[N])
    {
        if(next==pp)continue;
        sz[N]+=getSz(next,N);
    }
    return sz[N];
}
int get_centroid(int N,int pp,int cap)
{
    for(int next:v[N])
	{
        if(next==pp)continue;
        if(sz[next]>cap) return get_centroid(next,N,cap);
    }
    return N;
}
int cnt[200001],depth[200001];
void dfs(int N,int p=-1,int d=0){
	depth[N]=d;
	cnt[N]=1;
	for(int next:v[N]){
		if(next==p)continue;
		dfs(next,N,d+1);
		cnt[N]+=cnt[next];
	}
}
int find(int N,int p,int k){
	int res=-1;
	if(cnt[N]>=k)res=depth[N];
	for(int next:v[N]){
		if(next==p)continue;
		res=max(res,find(next,N,k));
	}
	return res;
}
int main(){
	int n;
	scanf("%d",&n);
	fup(i,1,n-1,1){
		int x,y;
		scanf("%d%d",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	int cap=getSz(n,-1)/2;
    int R=get_centroid(n,-1,cap);
	dfs(R);
	fup(i,1,n,1){
		if(i&1)puts("1");
		else{
			Pi r(-1,-1);
			for(int next:v[R]){
				int t=find(next,R,i/2);
				if(t>r.X){
					r.Y=r.X;
					r.X=t;
				}else if(t>r.Y){
					r.Y=t;
				}
			}
			if(r.Y==-1)puts("2");
			else printf("%d\n",r.X+r.Y+1);
		}
	}
}

Compilation message

meetings2.cpp: In function 'int main()':
meetings2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
meetings2.cpp:86:2: note: in expansion of macro 'fup'
   86 |  fup(i,1,n-1,1){
      |  ^~~
meetings2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
meetings2.cpp:95:2: note: in expansion of macro 'fup'
   95 |  fup(i,1,n,1){
      |  ^~~
meetings2.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
meetings2.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |   scanf("%d%d",&x,&y);
      |   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Incorrect 3 ms 4940 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Incorrect 3 ms 4940 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Incorrect 3 ms 4940 KB Output isn't correct
5 Halted 0 ms 0 KB -