답안 #441267

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
441267 2021-07-04T19:08:20 Z cheetose Meetings 2 (JOI21_meetings2) C++17
100 / 100
469 ms 49568 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];
VPi vv[200001];
int depth[200001],parent[200001][18];
int cnt[200001];
void dfs(int N,int p=-1){
	parent[N][0]=p;
	cnt[N]=1;
	for(int next:v[N]){
		if(next==p)continue;
		depth[next]=depth[N]+1;
		dfs(next,N);
		cnt[N]+=cnt[next];
	}
}
int LCA(int x, int y) {
	if (depth[x] < depth[y])swap(x, y);
	int dif = depth[x] - depth[y];
	for (int j = 0; dif > 0; j++) {
		if (dif & 1)x = parent[x][j];
		dif >>= 1;
	}

	if (x != y) {
		fdn(j,16,0,1) {
			if (parent[x][j] != -1 && parent[x][j] != parent[y][j]) {
				x = parent[x][j];
				y = parent[y][j];
			}
		}
		x = parent[x][0];
	}
	return x;
}
int dist(int x,int y){
	int u=LCA(x,y);
	return depth[x]+depth[y]-2*depth[u];
}
int cc,n;
void dfs2(int N,int p=-1){
	for(int next:v[N]){
		if(next==p)continue;
		vv[min(cnt[next],n-cnt[next])].pb(mp(N,next));
		dfs2(next,N);
	}
}
int ans[200001];
bool chk[200001];
int upd(int x,int &sz,int &a,int &b){
	chk[x]=1;
	sz++;
	if(sz==1){
		a=x;
		return 1;
	}else if(sz==2){
		b=x;
		return 2;
	}else{
		int d1=dist(a,b),d2=dist(a,x),d3=dist(b,x);
		int res=max({d1,d2,d3});
		if(d2==res)b=x;
		else if(d3==res)a=x;
		return res+1;
	}
}
int main(){
	MEM_1(parent);
	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);
	}
	dfs(1);
	fup(j,0,16,1)
		fup(i,1,n,1)
		if (parent[i][j] != -1)
			parent[i][j + 1] = parent[parent[i][j]][j];
	dfs2(1);
	int sz=0,r=1;
	int a=-1,b=-1;
	fdn(i,n,1,1){
		if(i&1)ans[i]=1;
		else{
			for(Pi P:vv[i/2]){
				if(!chk[P.X])r=upd(P.X,sz,a,b);
				if(!chk[P.Y])r=upd(P.Y,sz,a,b);
			}
			ans[i]=r;
		}
	}
	fup(i,1,n,1)printf("%d\n",ans[i]);
}

Compilation message

meetings2.cpp: In function 'int LCA(int, int)':
meetings2.cpp:11:30: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   11 | #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
      |                              ^
meetings2.cpp:66:3: note: in expansion of macro 'fdn'
   66 |   fdn(j,16,0,1) {
      |   ^~~
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:110:2: note: in expansion of macro 'fup'
  110 |  fup(i,1,n-1,1){
      |  ^~~
meetings2.cpp:10:30: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
meetings2.cpp:117:2: note: in expansion of macro 'fup'
  117 |  fup(j,0,16,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:118:3: note: in expansion of macro 'fup'
  118 |   fup(i,1,n,1)
      |   ^~~
meetings2.cpp:11:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   11 | #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
      |                              ^
meetings2.cpp:124:2: note: in expansion of macro 'fdn'
  124 |  fdn(i,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:134:2: note: in expansion of macro 'fup'
  134 |  fup(i,1,n,1)printf("%d\n",ans[i]);
      |  ^~~
meetings2.cpp:109:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
meetings2.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |   scanf("%d%d",&x,&y);
      |   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23756 KB Output is correct
3 Correct 14 ms 23700 KB Output is correct
4 Correct 13 ms 23772 KB Output is correct
5 Correct 13 ms 23756 KB Output is correct
6 Correct 14 ms 23716 KB Output is correct
7 Correct 15 ms 23704 KB Output is correct
8 Correct 14 ms 23756 KB Output is correct
9 Correct 14 ms 23692 KB Output is correct
10 Correct 14 ms 23748 KB Output is correct
11 Correct 13 ms 23756 KB Output is correct
12 Correct 13 ms 23760 KB Output is correct
13 Correct 13 ms 23696 KB Output is correct
14 Correct 13 ms 23756 KB Output is correct
15 Correct 14 ms 23756 KB Output is correct
16 Correct 14 ms 23788 KB Output is correct
17 Correct 14 ms 23676 KB Output is correct
18 Correct 14 ms 23796 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 13 ms 23788 KB Output is correct
21 Correct 13 ms 23708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23756 KB Output is correct
3 Correct 14 ms 23700 KB Output is correct
4 Correct 13 ms 23772 KB Output is correct
5 Correct 13 ms 23756 KB Output is correct
6 Correct 14 ms 23716 KB Output is correct
7 Correct 15 ms 23704 KB Output is correct
8 Correct 14 ms 23756 KB Output is correct
9 Correct 14 ms 23692 KB Output is correct
10 Correct 14 ms 23748 KB Output is correct
11 Correct 13 ms 23756 KB Output is correct
12 Correct 13 ms 23760 KB Output is correct
13 Correct 13 ms 23696 KB Output is correct
14 Correct 13 ms 23756 KB Output is correct
15 Correct 14 ms 23756 KB Output is correct
16 Correct 14 ms 23788 KB Output is correct
17 Correct 14 ms 23676 KB Output is correct
18 Correct 14 ms 23796 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 13 ms 23788 KB Output is correct
21 Correct 13 ms 23708 KB Output is correct
22 Correct 17 ms 24012 KB Output is correct
23 Correct 16 ms 23936 KB Output is correct
24 Correct 16 ms 24012 KB Output is correct
25 Correct 16 ms 23996 KB Output is correct
26 Correct 16 ms 23968 KB Output is correct
27 Correct 17 ms 24012 KB Output is correct
28 Correct 17 ms 24012 KB Output is correct
29 Correct 17 ms 24012 KB Output is correct
30 Correct 16 ms 24012 KB Output is correct
31 Correct 19 ms 24020 KB Output is correct
32 Correct 17 ms 24188 KB Output is correct
33 Correct 16 ms 24184 KB Output is correct
34 Correct 16 ms 24000 KB Output is correct
35 Correct 16 ms 23936 KB Output is correct
36 Correct 16 ms 24012 KB Output is correct
37 Correct 16 ms 23996 KB Output is correct
38 Correct 17 ms 24080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23756 KB Output is correct
3 Correct 14 ms 23700 KB Output is correct
4 Correct 13 ms 23772 KB Output is correct
5 Correct 13 ms 23756 KB Output is correct
6 Correct 14 ms 23716 KB Output is correct
7 Correct 15 ms 23704 KB Output is correct
8 Correct 14 ms 23756 KB Output is correct
9 Correct 14 ms 23692 KB Output is correct
10 Correct 14 ms 23748 KB Output is correct
11 Correct 13 ms 23756 KB Output is correct
12 Correct 13 ms 23760 KB Output is correct
13 Correct 13 ms 23696 KB Output is correct
14 Correct 13 ms 23756 KB Output is correct
15 Correct 14 ms 23756 KB Output is correct
16 Correct 14 ms 23788 KB Output is correct
17 Correct 14 ms 23676 KB Output is correct
18 Correct 14 ms 23796 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 13 ms 23788 KB Output is correct
21 Correct 13 ms 23708 KB Output is correct
22 Correct 17 ms 24012 KB Output is correct
23 Correct 16 ms 23936 KB Output is correct
24 Correct 16 ms 24012 KB Output is correct
25 Correct 16 ms 23996 KB Output is correct
26 Correct 16 ms 23968 KB Output is correct
27 Correct 17 ms 24012 KB Output is correct
28 Correct 17 ms 24012 KB Output is correct
29 Correct 17 ms 24012 KB Output is correct
30 Correct 16 ms 24012 KB Output is correct
31 Correct 19 ms 24020 KB Output is correct
32 Correct 17 ms 24188 KB Output is correct
33 Correct 16 ms 24184 KB Output is correct
34 Correct 16 ms 24000 KB Output is correct
35 Correct 16 ms 23936 KB Output is correct
36 Correct 16 ms 24012 KB Output is correct
37 Correct 16 ms 23996 KB Output is correct
38 Correct 17 ms 24080 KB Output is correct
39 Correct 296 ms 35040 KB Output is correct
40 Correct 286 ms 36940 KB Output is correct
41 Correct 298 ms 37688 KB Output is correct
42 Correct 303 ms 37684 KB Output is correct
43 Correct 307 ms 37696 KB Output is correct
44 Correct 305 ms 37856 KB Output is correct
45 Correct 469 ms 44680 KB Output is correct
46 Correct 371 ms 49568 KB Output is correct
47 Correct 261 ms 38184 KB Output is correct
48 Correct 215 ms 37828 KB Output is correct
49 Correct 345 ms 38244 KB Output is correct
50 Correct 240 ms 37940 KB Output is correct
51 Correct 296 ms 47616 KB Output is correct