Submission #571713

# Submission time Handle Problem Language Result Execution time Memory
571713 2022-06-02T15:12:04 Z bojackduy Torrent (COI16_torrent) C++14
100 / 100
438 ms 46076 KB
#include<iostream>
#include<queue>
#include<stack>
#include<algorithm>
#include<string.h>
#define task 
#define size() size() * 1ll 
#define all(x) (x).begin(), (x).end()
#define allr(x, sz) (x) + 1, (x) + 1 + sz
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define MASK(x) (1LL<<(x))
#define BIT(x,i) (((x)>>(i))&1)
#define numbit(x) __builtin_popcountll(x)

using namespace std;

const int N = 1e6 + 1;
const int M = 1e3 + 1;
const long long mod = 1e9 + 7;
const long long oo = 1e18 + 7;

typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;

template<class t>
bool mini(t &x,t y) {
    if (x > y) {
       x = y;
       return 1;
    }
return 0;
}
template<class t>
bool maxi(t &x,t y) {
    if (x < y) {
       x = y;
       return 1;
    }
return 0;
}
void file() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen(task.in, r, stdin);
    //freopen(task.out, w, stdout);
return ;
}

int n, a, b;
int cha[N], cl[N], dp[N];
vi adj[N];
void dfs(int u, int chu) {
    cha[u] = chu;
    for (auto v: adj[u]) if (v != chu) {
        dfs(v, u);
    }
}
void calc(int u, int chu, int clr) {
    dp[u] = 0;
    vi val;
    for (auto v: adj[u]) if (v != chu && cl[v] != clr) {
        calc(v, u, clr);
        val.pb(dp[v]);
    }
    sort(all(val), greater<int>());
    for (int i = 0; i < val.size(); i++) {
        maxi(dp[u], val[i] + i + 1);
    }
}
void solve(int test = -1) {
    cin >> n >> a >> b;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    dfs(a, 0);
    vi node;
    for (int i = b; i; i = cha[i]) {
        node.pb(i);
    }
    reverse(all(node));
    int l = 0, r = node.size() - 1;
    int res = l;
    while (l <= r) {
        int mid = l + r >> 1;
        for (int i = 0; i <= mid; i++) {
            cl[node[i]] = 1;
        }
        for (int i = mid + 1; i < node.size(); i++) {
            cl[node[i]] = 2;
        }
        calc(a, -1, 2);
        calc(b, -1, 1);
        if (dp[a] <= dp[b]) {
            l = mid + 1;
            res = mid;
        } else r = mid - 1;
    }
    for (int i = 0; i <= res; i++) {
        cl[node[i]] = 1;
    }
    for (int i = res + 1; i < node.size(); i++) {
        cl[node[i]] = 2;
    }
    calc(a, -1, 2);
    calc(b, -1, 1);
    int ans = max(dp[a], dp[b]);
    if (res + 1 < node.size()) {
        for (int i = 0; i <= res + 1; i++) {
            cl[node[i]] = 1;
        }
        for (int i = res + 2; i < node.size(); i++) {
            cl[node[i]] = 2;
        }
        calc(a, -1, 2);
        calc(b, -1, 1);
        mini(ans, max(dp[a], dp[b]));
    }
    cout << ans;
}

int32_t main()  {
    file();
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++) {
        solve(i);
    }
return 0;
}

Compilation message

torrent.cpp: In function 'void calc(int, int, int)':
torrent.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
   71 |     for (int i = 0; i < val.size(); i++) {
      |                       ^
torrent.cpp: In function 'void solve(int)':
torrent.cpp:92:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   92 |         int mid = l + r >> 1;
      |                   ~~^~~
torrent.cpp:96:33: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
   96 |         for (int i = mid + 1; i < node.size(); i++) {
      |                                 ^
torrent.cpp:109:29: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
  109 |     for (int i = res + 1; i < node.size(); i++) {
      |                             ^
torrent.cpp:115:17: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
  115 |     if (res + 1 < node.size()) {
      |                 ^
torrent.cpp:119:33: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
  119 |         for (int i = res + 2; i < node.size(); i++) {
      |                                 ^
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 13 ms 23812 KB Output is correct
3 Correct 14 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 438 ms 42444 KB Output is correct
2 Correct 408 ms 43744 KB Output is correct
3 Correct 427 ms 45492 KB Output is correct
4 Correct 435 ms 44724 KB Output is correct
5 Correct 403 ms 41988 KB Output is correct
6 Correct 401 ms 42704 KB Output is correct
7 Correct 388 ms 46076 KB Output is correct