結果

問題 No.2618 除霊
ユーザー TairitsuMeowTairitsuMeow
提出日時 2024-01-26 21:57:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 2,000 ms
コード長 1,653 bytes
コンパイル時間 1,848 ms
コンパイル使用メモリ 171,496 KB
実行使用メモリ 55,912 KB
最終ジャッジ日時 2024-01-26 21:57:36
合計ジャッジ時間 12,938 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
24,936 KB
testcase_01 AC 8 ms
24,940 KB
testcase_02 AC 8 ms
24,940 KB
testcase_03 AC 8 ms
24,940 KB
testcase_04 AC 9 ms
24,940 KB
testcase_05 AC 9 ms
24,940 KB
testcase_06 AC 189 ms
41,896 KB
testcase_07 AC 181 ms
41,204 KB
testcase_08 AC 211 ms
43,400 KB
testcase_09 AC 179 ms
43,216 KB
testcase_10 AC 184 ms
43,412 KB
testcase_11 AC 173 ms
38,940 KB
testcase_12 AC 222 ms
55,912 KB
testcase_13 AC 95 ms
38,172 KB
testcase_14 AC 120 ms
40,016 KB
testcase_15 AC 105 ms
39,304 KB
testcase_16 AC 112 ms
39,236 KB
testcase_17 AC 124 ms
41,672 KB
testcase_18 AC 117 ms
41,204 KB
testcase_19 AC 117 ms
39,632 KB
testcase_20 AC 130 ms
39,372 KB
testcase_21 AC 112 ms
41,152 KB
testcase_22 AC 144 ms
41,716 KB
testcase_23 AC 111 ms
39,588 KB
testcase_24 AC 108 ms
39,460 KB
testcase_25 AC 133 ms
41,672 KB
testcase_26 AC 134 ms
41,164 KB
testcase_27 AC 142 ms
39,328 KB
testcase_28 AC 110 ms
39,676 KB
testcase_29 AC 129 ms
41,668 KB
testcase_30 AC 143 ms
43,556 KB
testcase_31 AC 247 ms
53,420 KB
testcase_32 AC 169 ms
39,572 KB
testcase_33 AC 184 ms
41,444 KB
testcase_34 AC 199 ms
41,116 KB
testcase_35 AC 180 ms
43,340 KB
testcase_36 AC 177 ms
43,552 KB
testcase_37 AC 201 ms
43,244 KB
testcase_38 AC 212 ms
41,580 KB
testcase_39 AC 216 ms
41,576 KB
testcase_40 AC 220 ms
43,388 KB
testcase_41 AC 242 ms
43,576 KB
testcase_42 AC 212 ms
43,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Problem: No.2618 除霊
// Contest: yukicoder
// URL: https://yukicoder.me/problems/no/2618
// Memory Limit: 512 MB
// Time Limit: 2000 ms

#include<bits/stdc++.h>
#define debug(x) cerr<<(#x)<<" "<<(x)<<endl
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pii pair<ll,ll>
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
using namespace std;
bool Mbe; 
ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void write(ll x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
const ll N=5e5+9;
ll n,m,a[N],po[N],sum[N],tr[N],tag[N],ent[N],fa[N];
vector<ll>to[N];
void dfs(ll x,ll f){
	fa[x]=f;
	for(ll y:to[x]){
		if(y==f)continue;
		dfs(y,x);
		sum[x]+=tr[y];
		tr[x]+=tr[y];
		if(!ent[y]&&!tag[y]&&tag[x])tr[x]++;
		ent[x]+=tag[y];
	}
	tr[x]+=(ent[x]||tag[x]);
}
bool Med;
int main(){
	cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";
	n=read();
	rep(i,2,n){
		ll x=read(),y=read();
		to[x].push_back(y);
		to[y].push_back(x);
	}
	m=read();
	rep(i,1,m)a[i]=read(),tag[a[i]]=1;
	rep(i,1,m){
		po[a[i]]++;
		for(ll j:to[a[i]])po[j]++;
	}
	dfs(1,0);
	rep(i,1,n){
		ll res=tr[1]-(i==1?tr[1]:tr[fa[i]]);
		if(po[fa[i]]&&ent[fa[i]]+tag[fa[i]]==0)res--;
		if(fa[fa[i]]){
			ll c=po[fa[fa[i]]]-tag[fa[i]];
			if(!c&&po[fa[fa[i]]])res--;
		}
		for(ll j:to[i]){
			if(j==fa[i])continue;
			res+=sum[j]+(po[j]-tag[i]-tag[j]?1:0);
		}
		ll j=fa[i];
		if(j){
			res+=sum[j]-tr[i];
			if(po[j]-tag[i]-tag[j])res++;
		}
		write(res),putchar('\n');
	}
	return 0;
}
0