結果

問題 No.1718 Random Squirrel
ユーザー 👑 potato167potato167
提出日時 2021-10-24 02:14:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 2,681 bytes
コンパイル時間 2,594 ms
コンパイル使用メモリ 223,388 KB
実行使用メモリ 17,104 KB
最終ジャッジ日時 2024-04-08 19:15:38
合計ジャッジ時間 7,777 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 66 ms
8,832 KB
testcase_12 AC 17 ms
6,548 KB
testcase_13 AC 58 ms
7,936 KB
testcase_14 AC 70 ms
8,960 KB
testcase_15 AC 109 ms
11,616 KB
testcase_16 AC 41 ms
7,040 KB
testcase_17 AC 62 ms
8,448 KB
testcase_18 AC 170 ms
14,876 KB
testcase_19 AC 103 ms
10,956 KB
testcase_20 AC 20 ms
6,548 KB
testcase_21 AC 100 ms
11,136 KB
testcase_22 AC 219 ms
17,104 KB
testcase_23 AC 111 ms
11,264 KB
testcase_24 AC 100 ms
11,136 KB
testcase_25 AC 219 ms
17,100 KB
testcase_26 AC 47 ms
12,812 KB
testcase_27 AC 47 ms
12,392 KB
testcase_28 AC 46 ms
12,812 KB
testcase_29 AC 43 ms
11,008 KB
testcase_30 AC 46 ms
11,264 KB
testcase_31 AC 43 ms
11,264 KB
testcase_32 AC 43 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=1000000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"YES\n"; else cout<<"NO\n";}



//  rainy ~ 雨に打たれて ~
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int N,K;
    cin>>N>>K;
    vector<vector<int>> G(N);
    rep(i,N-1){
        int a,b;
        cin>>a>>b;
        a--,b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<int> D(K);
    set<int> var;
    rep(i,K) cin>>D[i],D[i]--,var.insert(D[i]);
    stack<int> s;
    vector<int> in(N,-2),out(N);
    int ind=-1;
    s.push(D[0]);
    rep(i,N*2){
        int a=s.top();
        s.pop();
        if(a>=N){
            out[a-N]=ind;
        }else{
            in[a]=ind;
            s.push(a+N);
            if(var.count(a)) ind=a;
            for(auto x:G[a]){
                if(in[x]==-2){
                    in[x]=-3;
                    s.push(x);
                }
            }
        }
    }
    vector<int> seen(N);
    vector<int> dis(N),ans(N,INF);
    queue<int> q;
    ind=D[0];
    rep(roop,3){
        rep(i,N) seen[i]=-1;
        q.push(ind);
        seen[ind]=0;
        while(!q.empty()){
            ind=q.front();
            q.pop();
            for(auto x:G[ind]){
                if(in[x]!=out[x]&&seen[x]==-1){
                    seen[x]=seen[ind]+1;
                    chmax(dis[x],seen[x]);
                    q.push(x);
                }
            }
        }
    }
    _pq<pair<int,int>> pq;
    int tmp=-2;
    rep(i,N) if(in[i]!=out[i]) tmp+=2;
    rep(i,K) ans[D[i]]=tmp-dis[D[i]],pq.push({ans[D[i]],D[i]});
    while(!pq.empty()){
        ind=pq.top().second;
        pq.pop();
        for(auto x:G[ind]){
            if(chmin(ans[x],ans[ind]+1)){
                pq.push({ans[x],x});
            }
        }
    }
    rep(i,N){
        cout<<ans[i]<<"\n";
    }
}

0