Submission #1369667


Source Code Expand

#include <cstdio>
#include <vector>
#include <stack>
#include <tuple>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
using namespace std;
template <class T> inline void setmax(T & a, T const & b) { a = max(a, b); }
template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }

vector<int> compute_dist(int root, vector<vector<int> > const & g) {
    int n = g.size();
    vector<int> dist(n, -1);
    stack<int> stk;
    dist[root] = 0;
    stk.push(root);
    while (not stk.empty()) {
        int i = stk.top(); stk.pop();
        for (int j : g[i]) if (dist[j] == -1) {
            dist[j] = dist[i] + 1;
            stk.push(j);
        }
    }
    return dist;
}
vector<int> compute_parent(int root, vector<vector<int> > const & g) {
    int n = g.size();
    vector<int> parent(n, -1);
    stack<int> stk;
    stk.push(root);
    while (not stk.empty()) {
        int i = stk.top(); stk.pop();
        for (int j : g[i]) if (parent[j] == -1 and j != root) {
            parent[j] = i;
            stk.push(j);
        }
    }
    return parent;
}

int main() {
    // input
    int n, x, y; scanf("%d%d%d", &n, &x, &y); -- x; -- y;
    vector<vector<int> > g(n);
    repeat (i, n-1) {
        int a, b; scanf("%d%d", &a, &b); -- a; -- b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<vector<int> > h(n);
    repeat (i, n-1) {
        int c, d; scanf("%d%d", &c, &d); -- c; -- d;
        h[c].push_back(d);
        h[d].push_back(c);
    }
    // solve
    vector<int> dist_h = compute_dist(y, h);
    vector<int> parent_h = compute_parent(y, h);
    vector<bool> escapable(n);
    repeat (i, n) for (int j : g[i]) {
        if (parent_h[i] != j
                and parent_h[j] != i
                and parent_h[i] != parent_h[j]
                and (parent_h[i] == -1 or parent_h[parent_h[i]] != j)
                and (parent_h[j] == -1 or parent_h[parent_h[j]] != i)) { // dist(i, j) >= 3
            escapable[i] = true;
            escapable[j] = true;
        }
    }
    int result = 0;
    vector<bool> used(n);
    stack<pair<int, int> > que;
    que.emplace(x, 0);
    while (not que.empty()) {
        int i, dist; tie(i, dist) = que.top(); que.pop();
        setmax(result, dist_h[i] * 2);
        if (escapable[i]) {
            result = -1;
            break;
        }
        for (int j : g[i]) if (not used[j]) {
            used[j] = true;
            if (dist_h[j] <= dist + 1) continue;
            que.emplace(j, dist + 1);
        }
    }
    // output
    printf("%d\n", result);
    return 0;
}

Submission Info

Submission Time
Task E - Sugigma: The Showdown
User kimiyuki
Language C++14 (GCC 5.4.1)
Score 1400
Code Size 2852 Byte
Status AC
Exec Time 182 ms
Memory 26740 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:43:45: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     int n, x, y; scanf("%d%d%d", &n, &x, &y); -- x; -- y;
                                             ^
./Main.cpp:46:40: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         int a, b; scanf("%d%d", &a, &b); -- a; -- b;
                                        ^
./Main.cpp:52:40: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         int c, d; scanf("%d%d", &c, &d); -- c; -- d;
                                        ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1400 / 1400
Status
AC × 5
AC × 103
Set Name Test Cases
Sample example0, example1, example2, example3, example4
All doublestar0, doublestar1, doublestar2, doublestar3, doublestar4, doublestar5, doublestar6, doublestar7, example0, example1, example2, example3, example4, giri0, giri1, giri2, giri3, giri4, giri5, giri6, giri7, giri8, giri9, maxrand0, maxrand1, maxrand2, maxrand3, maxrand4, maxrand5, maxrand6, maxrand7, maxrand8, maxrand9, narashi0, narashi1, narashi2, narashi3, narashi4, narashi5, narashi6, narashi7, narashi8, narashi9, ok0, ok1, ok2, ok3, ok4, ok5, ok6, ok7, ok8, ok9, ouh0, ouh1, ouh2, ouh3, ouh4, ouh5, ouh6, ouh7, ouh8, ouh9, same0, same1, same2, same3, same4, same5, same6, same7, same8, same9, sameline0, sameline1, sameline2, sameline3, sameline4, sameline5, sameline6, sameline7, sameline8, sameline9, star0, star1, star2, star3, star4, star5, star6, star7, star8, star9, supersmall0, supersmall1, supersmall2, supersmall3, supersmall4, supersmall5, supersmall6, supersmall7, supersmall8, supersmall9
Case Name Status Exec Time Memory
doublestar0 AC 138 ms 24180 KB
doublestar1 AC 129 ms 23032 KB
doublestar2 AC 153 ms 23800 KB
doublestar3 AC 130 ms 22776 KB
doublestar4 AC 143 ms 24568 KB
doublestar5 AC 150 ms 24696 KB
doublestar6 AC 126 ms 22776 KB
doublestar7 AC 132 ms 23032 KB
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
example3 AC 1 ms 256 KB
example4 AC 1 ms 256 KB
giri0 AC 149 ms 24576 KB
giri1 AC 155 ms 23168 KB
giri2 AC 158 ms 23424 KB
giri3 AC 164 ms 23296 KB
giri4 AC 166 ms 23296 KB
giri5 AC 161 ms 24064 KB
giri6 AC 159 ms 23936 KB
giri7 AC 160 ms 23552 KB
giri8 AC 160 ms 23552 KB
giri9 AC 165 ms 23808 KB
maxrand0 AC 176 ms 24064 KB
maxrand1 AC 169 ms 23808 KB
maxrand2 AC 161 ms 24192 KB
maxrand3 AC 159 ms 23552 KB
maxrand4 AC 152 ms 23168 KB
maxrand5 AC 154 ms 23168 KB
maxrand6 AC 155 ms 23168 KB
maxrand7 AC 156 ms 23808 KB
maxrand8 AC 161 ms 23040 KB
maxrand9 AC 161 ms 23680 KB
narashi0 AC 150 ms 22784 KB
narashi1 AC 158 ms 22912 KB
narashi2 AC 145 ms 23040 KB
narashi3 AC 153 ms 23552 KB
narashi4 AC 163 ms 23680 KB
narashi5 AC 153 ms 23680 KB
narashi6 AC 158 ms 23424 KB
narashi7 AC 155 ms 24064 KB
narashi8 AC 164 ms 23808 KB
narashi9 AC 160 ms 23168 KB
ok0 AC 156 ms 23164 KB
ok1 AC 156 ms 23548 KB
ok2 AC 155 ms 23548 KB
ok3 AC 171 ms 23804 KB
ok4 AC 152 ms 23168 KB
ok5 AC 161 ms 23804 KB
ok6 AC 182 ms 23420 KB
ok7 AC 158 ms 23040 KB
ok8 AC 174 ms 23932 KB
ok9 AC 171 ms 22908 KB
ouh0 AC 132 ms 24208 KB
ouh1 AC 165 ms 24480 KB
ouh2 AC 139 ms 22236 KB
ouh3 AC 148 ms 23880 KB
ouh4 AC 155 ms 24048 KB
ouh5 AC 151 ms 22396 KB
ouh6 AC 158 ms 22780 KB
ouh7 AC 152 ms 23600 KB
ouh8 AC 160 ms 23548 KB
ouh9 AC 153 ms 23036 KB
same0 AC 161 ms 23680 KB
same1 AC 156 ms 23424 KB
same2 AC 156 ms 24064 KB
same3 AC 155 ms 24064 KB
same4 AC 151 ms 23168 KB
same5 AC 162 ms 23168 KB
same6 AC 152 ms 23552 KB
same7 AC 162 ms 23168 KB
same8 AC 147 ms 23040 KB
same9 AC 165 ms 23936 KB
sameline0 AC 154 ms 22784 KB
sameline1 AC 164 ms 23680 KB
sameline2 AC 158 ms 23040 KB
sameline3 AC 163 ms 23040 KB
sameline4 AC 157 ms 23552 KB
sameline5 AC 164 ms 23040 KB
sameline6 AC 164 ms 22912 KB
sameline7 AC 161 ms 23296 KB
sameline8 AC 181 ms 23552 KB
sameline9 AC 162 ms 23680 KB
star0 AC 107 ms 25204 KB
star1 AC 105 ms 25972 KB
star2 AC 101 ms 25204 KB
star3 AC 109 ms 25076 KB
star4 AC 107 ms 25332 KB
star5 AC 118 ms 26356 KB
star6 AC 104 ms 25460 KB
star7 AC 106 ms 25204 KB
star8 AC 108 ms 25076 KB
star9 AC 114 ms 26740 KB
supersmall0 AC 1 ms 256 KB
supersmall1 AC 1 ms 256 KB
supersmall2 AC 1 ms 256 KB
supersmall3 AC 1 ms 256 KB
supersmall4 AC 1 ms 256 KB
supersmall5 AC 1 ms 256 KB
supersmall6 AC 1 ms 256 KB
supersmall7 AC 1 ms 256 KB
supersmall8 AC 1 ms 256 KB
supersmall9 AC 1 ms 256 KB