結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー hiro71687khiro71687k
提出日時 2023-03-28 20:14:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 243 ms / 2,000 ms
コード長 1,431 bytes
コンパイル時間 5,192 ms
コンパイル使用メモリ 267,176 KB
実行使用メモリ 19,116 KB
最終ジャッジ日時 2023-10-20 13:27:31
合計ジャッジ時間 11,700 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 5 ms
4,348 KB
testcase_05 AC 14 ms
4,416 KB
testcase_06 AC 15 ms
4,544 KB
testcase_07 AC 9 ms
4,348 KB
testcase_08 AC 17 ms
4,716 KB
testcase_09 AC 14 ms
4,680 KB
testcase_10 AC 14 ms
4,440 KB
testcase_11 AC 12 ms
4,416 KB
testcase_12 AC 11 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 165 ms
14,380 KB
testcase_15 AC 158 ms
13,852 KB
testcase_16 AC 145 ms
13,208 KB
testcase_17 AC 98 ms
10,752 KB
testcase_18 AC 193 ms
15,436 KB
testcase_19 AC 229 ms
17,284 KB
testcase_20 AC 225 ms
17,284 KB
testcase_21 AC 234 ms
17,284 KB
testcase_22 AC 243 ms
17,284 KB
testcase_23 AC 225 ms
17,284 KB
testcase_24 AC 229 ms
17,284 KB
testcase_25 AC 234 ms
17,284 KB
testcase_26 AC 236 ms
17,284 KB
testcase_27 AC 230 ms
17,284 KB
testcase_28 AC 226 ms
17,284 KB
testcase_29 AC 145 ms
15,964 KB
testcase_30 AC 146 ms
15,964 KB
testcase_31 AC 117 ms
19,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll inf=14449999999999994;
ll mod=1000000007;
int main(){
    ll n;
    cin >> n;
    vector<vector<ll>>g(n);
    for (ll i = 0; i < n-1; i++)
    {
        ll a,b;
        cin >> a >> b;a--,b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<ll>memo(n,inf);
    memo[0]=0;
    queue<ll>que;
    que.push(0);
    while (!que.empty())
    {
        ll v=que.front();
        que.pop();
        for (ll i = 0; i < g[v].size(); i++)
        {
            if (memo[g[v][i]]>memo[v]+1)
            {
                memo[g[v][i]]=memo[v]+1;
                que.push(g[v][i]);
            }
        }
    }
    ll id=0,x=0;
    for (ll i = 0; i < n; i++)
    {
        if (memo[i]>x)
        {
            id=i;
            x=memo[i];
        }
    }
    que.push(id);
    for (ll i = 0; i < n; i++)
    {
        memo[i]=inf;
    }
    memo[id]=0;
    while (!que.empty())
    {
        ll v=que.front();
        que.pop();
        for (ll i = 0; i < g[v].size(); i++)
        {
            if (memo[g[v][i]]>memo[v]+1)
            {
                memo[g[v][i]]=memo[v]+1;
                que.push(g[v][i]);
            }
        }
    }
    x=0;
    for (ll i = 0; i < n ; i++)
    {
        x=max(x,memo[i]);
    }
    cout << 2*(n-1)-x << endl;
}
0