제출 #1102900

#제출 시각아이디문제언어결과실행 시간메모리
1102900KasymKHard route (IZhO17_road)C++17
52 / 100
221 ms1108 KiB
#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss secox
#define all(v) v.begin(), v.ex()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 5005;
vector<int> adj[N];
int answer, sm, dp[N], odp[N];

void wow(int x, int pr = -1){
    dp[x] = odp[x] = 0;
    tr(it, adj[x]){
        int to = (*it);
        if(to != pr){
            wow(to, x);
            int same = dp[x];
            if(umax(same, dp[to]+1))
                odp[x] = dp[x], dp[x] = same;
            else if(odp[x] < dp[to]+1)
                odp[x] = dp[to]+1;
        }
    }
}

void dfs(int x, int pr, int dis, int mx){
    if(~pr and (int)adj[x].size()==1){
        if(umax(answer, dis*mx))
            sm = 1;
        else if(dis*mx == answer)
            sm++;
        return; 
    }
    tr(it, adj[x]){
        int to = (*it);
        if(to != pr){
            if(dp[to]+1 == dp[x])    
                dfs(to, x, dis+1, max(odp[x],mx));
            else
                dfs(to, x, dis+1, max(dp[x],mx));
        }
    }
}

int main(){
    int n;
    scanf("%d", &n);
    for(int i = 1; i < n; ++i){
        int a, b;
        scanf("%d%d", &a, &b);
        adj[a].pb(b);
        adj[b].pb(a);
    }
    vector<int> A;
    for(int i = 1; i <= n; ++i)
        if((int)adj[i].size()==1)
            A.pb(i);
    for(int &to : A){
        wow(to);
        dfs(to, -1, 0, 0);
    }
    printf("%d %d\n", answer, sm>>1);
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

road.cpp: In function 'int main()':
road.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
road.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         scanf("%d%d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...