結果

問題 No.806 木を道に
ユーザー face4face4
提出日時 2019-03-22 21:37:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,018 bytes
コンパイル時間 865 ms
コンパイル使用メモリ 83,408 KB
実行使用メモリ 9,860 KB
最終ジャッジ日時 2023-10-19 06:35:38
合計ジャッジ時間 2,941 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 39 ms
6,504 KB
testcase_25 AC 58 ms
8,000 KB
testcase_26 AC 21 ms
5,428 KB
testcase_27 AC 69 ms
9,860 KB
testcase_28 AC 4 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:44:19: warning: 'cost' may be used uninitialized [-Wmaybe-uninitialized]
   44 |     cout << n-1 - cost << endl;
      |                   ^~~~
main.cpp:33:9: note: 'cost' was declared here
   33 |     int cost;
      |         ^~~~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_algobase.h:64,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/string:50,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/locale_classes.h:40,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/ios_base.h:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ios:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ostream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/iostream:39,
                 from main.cpp:1:
In constructor 'constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U1 = int&; _U2 = int; typename std::enable_if<(std::_PCC<true, _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<true, _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> = true; _T1 = int; _T2 = int]',
    inlined from 'int main()' at main.cpp:32:12:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_pair.h:535:11: warning: 't' may be used uninitialized [-Wmaybe-uninitialized]
  535 |         : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:19:9: note: 't' was declared here
   19 |     int t;
      |         ^

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> v[n];
    int a, b;
    for(int i = 0; i < n-1; i++){
        cin >> a >> b;
        a--, b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    vector<bool> visit(n, 0);
    queue<int> q;
    int t;
    q.push(0);
    while(!q.empty()){
        int x = q.front();  q.pop();
        if(visit[x])    continue;
        t = x;
        visit[x] = true;
        for(int next : v[x]){
            if(!visit[next])    q.push(next);
        }
    }
    for(int i = 0; i < n; i++)  visit[i] = false;
    queue<pair<int,int>> pq;
    pq.push({t, 0});
    int cost;
    while(!pq.empty()){
        auto x = pq.front(); pq.pop();
        int i = x.first;
        if(visit[i])    continue;
        cost = x.second;
        visit[i] = true;
        for(int next : v[i]){
            if(!visit[next])    pq.push({next, cost+1});
        }
    }
    cout << n-1 - cost << endl;
    return 0;
}
0