結果

問題 No.806 木を道に
ユーザー lightninglightning
提出日時 2019-03-22 22:20:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,959 bytes
コンパイル時間 2,097 ms
コンパイル使用メモリ 178,992 KB
実行使用メモリ 10,636 KB
最終ジャッジ日時 2023-10-19 09:38:47
合計ジャッジ時間 4,118 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,732 KB
testcase_25 AC 57 ms
8,564 KB
testcase_26 AC 22 ms
5,540 KB
testcase_27 AC 68 ms
10,636 KB
testcase_28 AC 4 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define Rep(i,n) for(int i=0;i<n;i++)
#define For(i,n1,n2) for(int i=n1;i<n2;i++)
#define REP(i,n) for(ll i=0;i<n;i++)
#define FOR(i,n1,n2) for(ll i=n1;i<n2;i++)
#define put(a) cout<<a<<endl;
#define all(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define TDARRAY(int,a,n,m) vector<vector<int>> a(n,vector<int>(m,0));
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template<class T> inline bool chmax(T& a, T b) {if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a, T b) {if(a>b){a=b;return 1;}return 0;}

int n;
vector<int> a;
vector<int> b;
vector<vector<int>> e;
vector<int> visited;
int dfs(int c,int dis=0){
    int m=0;
    REP(i,e[c].size()){
        if(!visited[e[c][i]]){
            visited[e[c][i]]=1;
            chmax(m,dfs(e[c][i],dis+1));
            //return dfs(e[c],dis+1);
        }
    }
    return m;
}

int main(){
    cin >> n;
    a.resize(n-1);
    b.resize(n-1);
    e.resize(n);
    REP(i,n-1){
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
        e[a[i]].push_back(b[i]);
        e[b[i]].push_back(a[i]);
        
    }
    visited.resize(n,0);
    queue<int> q;
    q.push(0);
    visited[0]=1;
    int maxv=0;
    while(!q.empty()){
        int v = q.front();q.pop();
        REP(i,e[v].size()){
            if(!visited[e[v][i]]){
                visited[e[v][i]]=visited[v]+1;
                maxv=e[v][i];
                q.push(e[v][i]);
            }
        }
    }
    //int d = dfs(0);
    //visited.resize(n,0);
    REP(i,n){
        visited[i]=0;
    }
    q.push(maxv);
    visited[maxv]=1;
    int m=0;
    while(!q.empty()){
        int v = q.front();q.pop();
        REP(i,e[v].size()){
            if(!visited[e[v][i]]){
                visited[e[v][i]]=visited[v]+1;
                chmax(m,visited[e[v][i]]);
                q.push(e[v][i]);
            }
        }
    }
    put(n-m);
    return 0;
}
0