結果

問題 No.2427 Tree Distance Two
ユーザー nononnonon
提出日時 2023-08-18 21:43:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 694 ms / 2,000 ms
コード長 2,977 bytes
コンパイル時間 4,288 ms
コンパイル使用メモリ 267,164 KB
実行使用メモリ 35,120 KB
最終ジャッジ日時 2024-05-06 03:40:54
合計ジャッジ時間 16,282 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,496 KB
testcase_01 AC 5 ms
10,496 KB
testcase_02 AC 6 ms
10,624 KB
testcase_03 AC 661 ms
31,488 KB
testcase_04 AC 6 ms
10,368 KB
testcase_05 AC 694 ms
31,360 KB
testcase_06 AC 6 ms
10,496 KB
testcase_07 AC 621 ms
31,564 KB
testcase_08 AC 636 ms
35,120 KB
testcase_09 AC 612 ms
31,360 KB
testcase_10 AC 617 ms
32,640 KB
testcase_11 AC 668 ms
33,556 KB
testcase_12 AC 636 ms
32,656 KB
testcase_13 AC 617 ms
32,288 KB
testcase_14 AC 595 ms
29,312 KB
testcase_15 AC 6 ms
10,496 KB
testcase_16 AC 6 ms
10,624 KB
testcase_17 AC 5 ms
10,624 KB
testcase_18 AC 6 ms
10,496 KB
testcase_19 AC 5 ms
10,496 KB
testcase_20 AC 20 ms
11,264 KB
testcase_21 AC 22 ms
11,264 KB
testcase_22 AC 9 ms
10,624 KB
testcase_23 AC 8 ms
10,624 KB
testcase_24 AC 19 ms
11,264 KB
testcase_25 AC 160 ms
16,000 KB
testcase_26 AC 435 ms
23,808 KB
testcase_27 AC 293 ms
19,968 KB
testcase_28 AC 661 ms
29,824 KB
testcase_29 AC 560 ms
27,520 KB
testcase_30 AC 369 ms
23,040 KB
testcase_31 AC 234 ms
18,688 KB
testcase_32 AC 377 ms
22,912 KB
testcase_33 AC 327 ms
21,504 KB
testcase_34 AC 96 ms
13,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using db = long double;
using ch = char;
using bl = bool;
using st = string;
using pll = pair<ll,ll>;
using psl = pair<st,ll>;
using vst = vector<st>;
using vch = vector<ch>;
using vvch = vector<vch>;
using vbl = vector<bl>;
using vvbl = vector<vbl>;
using vdb = vector<db>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vpsl = vector<psl>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vvvvvll = vector<vvvvll>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
#define all(A) A.begin(),A.end()
#define sz(A) (ll)A.size()
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rrep(i,a,b) for(ll i=(ll)(a);i<=(ll)(b);i++)
#define drep(i,n) for(ll i=(ll)n-1; i>=0; i--)
#define drrep(i,a,b) for(ll i=(ll)a; i>=(ll)b; i--)

using mint = modint998244353;
// using mint = modint1000000007;
using vm = vector<mint>;
using vvm = vector<vm>; 
using vvvm = vector<vvm>;

const ll mod = 998244353;
const ll INF = 3e18;

template<class T> inline bool chmin(T &a, T b)
{
    if (a>b)
    {
        a=b;
        return true;
    }
    return false;
}

template<class T> inline bool chmax(T &a, T b)
{
    if (a<b)
    {
        a=b;
        return true;
    }
    return false;
}

ll gcd(ll a, ll b)
{
    if (a==0)return b;
    if (b==0)return a;
    ll c=a;
    while(a%b!=0)
    {
        c=a%b;
        a=b;
        b=c;
    }
    return b;
}

ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}

ll Pow(ll a, ll n)
{
    ll res=1;
    while(n>0)
    {
        if(n%2==1)res*=a;
        a*=a;
        n/=2;
    }
    return res;
}

ll Pow(ll a, ll n, ll p)
{
    ll res=1;
    while(n>0)
    {
        a%=p;
        if(n%2==1)res*=a;
        a*=a;
        res%=p;
        n/=2;
    }
    return res;
}

void yn(bl ok)
{
    cout << (ok?"Yes":"No") << endl;
    return;
}

ll N,dist[300010],chi[300010],par[300010],ans[300010];
vll G[300010];

int main()
{
    cin>>N;
    rep(i,N-1)
    {
        ll u,v; cin>>u>>v;
        u--, v--;
        G[u].pb(v);
        G[v].pb(u);
    }
    queue<ll> q;
    vbl vis(N,0);
    rep(i,N)dist[i]=INF;
    q.push(0);
    vis[0]=1;
    dist[0]=0;
    while(!q.empty())
    {
        ll v=q.front();
        q.pop();
        for(ll u:G[v])
        {
            if(vis[u])continue;
            chi[v]++;
            par[u]=v;
            q.push(u);
            dist[u]=dist[v]+1;
            vis[u]=1;
        }
    }
    rep(i,N)
    {
        if(dist[i]>=2)ans[i]=1;
        if(dist[i]>=1)ans[i]+=(chi[par[i]]-1);
        for(ll j:G[i])if(j!=par[i])ans[i]+=chi[j];
    }
    rep(i,N)cout << ans[i] << endl;
    return 0;
}
0