結果

問題 No.1718 Random Squirrel
ユーザー 👑 kmjpkmjp
提出日時 2021-10-23 00:13:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 2,286 bytes
コンパイル時間 2,434 ms
コンパイル使用メモリ 216,596 KB
実行使用メモリ 23,708 KB
最終ジャッジ日時 2023-10-24 16:34:11
合計ジャッジ時間 7,303 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,864 KB
testcase_01 AC 3 ms
10,864 KB
testcase_02 AC 3 ms
10,864 KB
testcase_03 AC 4 ms
10,864 KB
testcase_04 AC 3 ms
10,864 KB
testcase_05 AC 4 ms
10,864 KB
testcase_06 AC 3 ms
10,864 KB
testcase_07 AC 3 ms
10,864 KB
testcase_08 AC 3 ms
10,864 KB
testcase_09 AC 3 ms
10,864 KB
testcase_10 AC 3 ms
10,864 KB
testcase_11 AC 123 ms
13,600 KB
testcase_12 AC 26 ms
11,480 KB
testcase_13 AC 81 ms
12,544 KB
testcase_14 AC 126 ms
13,864 KB
testcase_15 AC 119 ms
13,624 KB
testcase_16 AC 57 ms
12,124 KB
testcase_17 AC 114 ms
13,660 KB
testcase_18 AC 160 ms
14,672 KB
testcase_19 AC 113 ms
13,432 KB
testcase_20 AC 46 ms
11,948 KB
testcase_21 AC 172 ms
14,980 KB
testcase_22 AC 196 ms
15,468 KB
testcase_23 AC 177 ms
14,920 KB
testcase_24 AC 174 ms
14,980 KB
testcase_25 AC 197 ms
15,464 KB
testcase_26 AC 154 ms
15,632 KB
testcase_27 AC 155 ms
15,636 KB
testcase_28 AC 158 ms
16,704 KB
testcase_29 AC 168 ms
18,888 KB
testcase_30 AC 176 ms
23,708 KB
testcase_31 AC 170 ms
22,012 KB
testcase_32 AC 168 ms
18,888 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void solve()':
main.cpp:82:29: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
   82 |         auto v=diameter(root);
      |                             ^
main.cpp:63:13: note: 'root' was declared here
   63 |         int root;
      |             ^~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,K;
vector<int> E[202020];
vector<int> D[2];
int G[202020];
int dep[202020],from[202020];
int ret[202020];

int dfs(int cur,int pre) {
	FORR(e,E[cur]) if(e!=pre) G[cur]|=dfs(e,cur);
	return G[cur];
}

pair<int,int> farthest(int cur,int pre,int d,vector<int>& D) {
	D[cur]=d;
	pair<int,int> r={d,cur};
	FORR(e,E[cur]) if(e!=pre&&G[e]) r=max(r, farthest(e,cur,d+1,D));
	return r;
}

//両端と間の頂点を返す
vector<pair<int,int>> diameter(int root) { // diameter,center
	D[0].resize(N);
	D[1].resize(N);
	auto v1=farthest(root,root,0,D[0]);
	auto v2=farthest(v1.second,v1.second,0,D[0]);
	farthest(v2.second,v2.second,0,D[1]);
	
	vector<pair<int,int>> R;
	for(int i=N-1;i>=0;i--) if(D[0][i]+D[1][i]==v2.first) R.push_back({D[0][i],i});
	sort(ALL(R));

	return R;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	FOR(i,N-1) {
		cin>>x>>y;
		E[x-1].push_back(y-1);
		E[y-1].push_back(x-1);
	}
	FOR(i,K) {
		cin>>x;
		G[x-1]=1;
	}
	int root;
	FOR(i,N) if(G[i]==1) root=i;
	dfs(root,root);
	
	queue<int> Q;
	FOR(i,N) {
		if(G[i]==1) from[i]=i, Q.push(i);
		else dep[i]=1<<20;
	}
	while(Q.size()) {
		int cur=Q.front();
		Q.pop();
		FORR(e,E[cur]) if(dep[e]>dep[cur]+1) {
			dep[e]=dep[cur]+1;
			from[e]=from[cur];
			Q.push(e);
		}
	}
	
	auto v=diameter(root);
	int len=0;
	FOR(i,N) if(G[i]) FORR(e,E[i]) len+=G[e];
	FOR(i,N) if(G[i]) {
		ret[i]=len-max(D[0][i],D[1][i]);
	}
	FOR(i,N) {
		if(G[i]==0) ret[i]=dep[i]+ret[from[i]];
		cout<<ret[i]<<endl;
	}
	
	
		
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0