Submission #1067746

# Submission time Handle Problem Language Result Execution time Memory
1067746 2024-08-21T02:47:56 Z 12345678 Torrent (COI16_torrent) C++17
31 / 100
27 ms 16220 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int nx=1e5+5;
 
int n, a, b, u, v, dp[nx], res=INT_MAX;
vector<pair<int, int>> d[nx];
pair<int, int> pa[nx], mn={INT_MAX, 0};
vector<int> cut, cost;
 
void dfspa(int u, int p)
{
    for (auto [v, idx]:d[u]) if (v!=p) pa[v]={u, idx}, dfspa(v, u);
}
 
void dfs(int u, int p, int c)
{
    dp[u]=0;
    vector<int> s;
    for (auto [v, idx]:d[u]) if (v!=p&&idx!=c) dfs(v, u, c), s.push_back(dp[v]);
    sort(s.begin(), s.end());
    reverse(s.begin(), s.end());
    for (int i=0; i<s.size(); i++) dp[u]=max(dp[u], i+1+s[i]);
}
 
int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>a>>b;
    for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back({v, i}), d[v].push_back({u, i});
    dfspa(a, a);
    int tmp=b;
    while (tmp!=a) cut.push_back(pa[tmp].second), tmp=pa[tmp].first;
    for (auto x:cut)
    {
        dfs(a, a, x);
        dfs(b, b, x);
        cost.push_back(max(dp[a], dp[b]));
    }
    for (int i=0; i<cost.size(); i++)
    {
        if (i==cost.size()-1) return cout<<cost[i], 0;
        else if (i!=0&&cost[i]>cost[i-1]) return cout<<cost[i-1], 0;
    }
}

Compilation message

torrent.cpp: In function 'void dfs(int, int, int)':
torrent.cpp:24:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for (int i=0; i<s.size(); i++) dp[u]=max(dp[u], i+1+s[i]);
      |                   ~^~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:41:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for (int i=0; i<cost.size(); i++)
      |                   ~^~~~~~~~~~~~
torrent.cpp:43:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |         if (i==cost.size()-1) return cout<<cost[i], 0;
      |             ~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2652 KB Output is correct
2 Correct 6 ms 2788 KB Output is correct
3 Correct 6 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 27 ms 16220 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -