답안 #997622

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
997622 2024-06-12T15:02:28 Z fuad27 Hard route (IZhO17_road) C++17
0 / 100
2 ms 13888 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 500100;
vector<int> g[N];
int n;
pair<int,int> dp[N];
pair<long long, long long> ans={0, 1};
void dfs1(int at, int p) {
  dp[at] = {1, 1};
  for(int to:g[at]) {
    if(to==p)continue;
    dfs1(to, at);
    if(dp[to].first+1 > dp[at].first) {
      dp[at]=dp[to];
      dp[at].first++;
    }
    else if(dp[to].first+1 == dp[at].first) {
      dp[at].second+=dp[to].second;
    }
  }
}

void dfs2(int at, int p) {
  sort(g[at].begin(), g[at].end(), [&](int u, int v) {
    return dp[u].first > dp[v].first;
  });
  if(g[at].size()>=3) {
    long long a = dp[g[at][0]].first, b = dp[g[at][1]].first, c = dp[g[at][2]].first;
    long long res = a*(b+c);
    long long cntb = 0, cntc = 0, totcnt=0;
    for(int to:g[at]) {
      if(b == dp[to].first) cntb++;
      if(c == dp[to].first) cntc++;
    }

    if(b == c) {
      totcnt = cntb*(cntb-1)/2;
    }
    else {
      totcnt = cntb*cntc;
    }
    if(ans.first == res)ans.second+=totcnt;
    else if(ans.first < res) {
      ans.first = res;
      ans.second = totcnt;
    }
  }
  pair<long long, long long> fr = {1, 1};
  pair<long long, long long> sc = {1, 1};
  for(int to:g[at]) {
    if(fr.first < dp[to].first+1) {
      fr=dp[to];
      fr.first++;
    }
    else if(fr.first == dp[to].first+1) {
      fr.second+=dp[to].second;
    }
  }
  for(int to:g[at]) {
    if(dp[to].first+1==fr.first)continue;
    if(sc.first < dp[to].first+1) {
      sc=dp[to];
      sc.first++;
    }
    else if(sc.first == dp[to].first+1) {
      sc.second+=dp[to].second;
    }
  }
  for(int to:g[at]) {
    if(to==p)continue;
    if(fr.first == dp[to].first+1 and fr.second  == dp[to].second) {
      dp[at] = sc;
    }
    else if(fr.first == dp[to].first+1) {
      dp[at] = fr;
      dp[at].second -= dp[to].second;
    }
    else {
      dp[at] = fr;
    }
    dfs2(to, at);
  }
}
int main () {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n;
  for(int i = 1;i<n;i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  cout << ans.first << " " << ans.second << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13660 KB Output is correct
2 Correct 2 ms 13884 KB Output is correct
3 Correct 2 ms 13884 KB Output is correct
4 Correct 2 ms 13660 KB Output is correct
5 Correct 2 ms 13660 KB Output is correct
6 Correct 2 ms 13884 KB Output is correct
7 Incorrect 2 ms 13888 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13660 KB Output is correct
2 Correct 2 ms 13884 KB Output is correct
3 Correct 2 ms 13884 KB Output is correct
4 Correct 2 ms 13660 KB Output is correct
5 Correct 2 ms 13660 KB Output is correct
6 Correct 2 ms 13884 KB Output is correct
7 Incorrect 2 ms 13888 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13660 KB Output is correct
2 Correct 2 ms 13884 KB Output is correct
3 Correct 2 ms 13884 KB Output is correct
4 Correct 2 ms 13660 KB Output is correct
5 Correct 2 ms 13660 KB Output is correct
6 Correct 2 ms 13884 KB Output is correct
7 Incorrect 2 ms 13888 KB Output isn't correct
8 Halted 0 ms 0 KB -