Submission #441248

# Submission time Handle Problem Language Result Execution time Memory
441248 2021-07-04T18:27:31 Z cheetose Meetings 2 (JOI21_meetings2) C++17
0 / 100
4 ms 5764 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 dist[200001];
int bfs(int N){
	MEM_1(dist);
	queue<int> q;
	q.push(N);
	dist[N]=0;
	int last;
	while(!q.empty()){
		int x=q.front();
		q.pop();
		last=x;
		for(int y:v[x]){
			if(dist[y]==-1){
				dist[y]=dist[x]+1;
				q.push(y);
			}
		}
	}
	return last;
}
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 t=bfs(1);
	t=bfs(t);
	int R=dist[t];
	fup(i,1,n,1){
		if(dist[i]==R/2){
			R=i;
			break;
		}
	}
	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: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:102:2: note: in expansion of macro 'fup'
  102 |  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);
      |   ~~~~~^~~~~~~~~~~~~~
meetings2.cpp: In function 'int bfs(int)':
meetings2.cpp:62:9: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
   62 |  return last;
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 4 ms 5708 KB Output is correct
3 Correct 4 ms 5764 KB Output is correct
4 Correct 4 ms 5708 KB Output is correct
5 Incorrect 4 ms 5708 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 4 ms 5708 KB Output is correct
3 Correct 4 ms 5764 KB Output is correct
4 Correct 4 ms 5708 KB Output is correct
5 Incorrect 4 ms 5708 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 4 ms 5708 KB Output is correct
3 Correct 4 ms 5764 KB Output is correct
4 Correct 4 ms 5708 KB Output is correct
5 Incorrect 4 ms 5708 KB Output isn't correct
6 Halted 0 ms 0 KB -