Submission #101450

#TimeUsernameProblemLanguageResultExecution timeMemory
101450dantoh000007 (CEOI14_007)C++14
0 / 100
263 ms30140 KiB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200001;
const int logn = 18;
int p[logn+1][maxn], h[maxn];
int H[maxn];
int d[maxn];
vector<int> adjlist[maxn];
bitset<maxn> vis;
void dfs(int x){
    if (vis[x]) return;
    vis[x] = 1;
    for (int k = 0; k < logn; k++){
        if (p[k][x] == -1) break;
        p[k+1][x] = p[k][p[k][x]];
    }
    for (auto v : adjlist[x]){
        if (vis[v]) continue;
        p[0][v] = x;
        d[v] = d[x]+1;
        h[v] = h[x]+1;
        dfs(v);
    }
}
int lca(int a, int b){
    if (h[a] > h[b]) swap(a,b);
    for (int k = 0, d= h[b] - h[a]; k < logn; k++){
        if (d & (1<<k)) b = p[k][b];
    }
    if (a==b) return a;
    assert(h[a] == h[b]);
    for (int k = logn-1; k >=0; --k){
        if (p[k][a] != p[k][b])
            a = p[k][a], b = p[k][b];
        assert(p[0][a] == p[0][b]);
        return p[0][a];
    }
}
int anc(int x, int d){
    for (int k = 0; k <= logn && x != -1; k++){
        if (d & (1<<k)) x = p[k][x];
    }
    return x;
}
int dist(int a, int b){
    return d[a] + d[b] - 2*d[lca(a,b)];
}
int main(){
    int n,e; scanf("%d%d",&n,&e);
    int s0, sn, serve1, serve2;
    scanf("%d%d%d%d",&s0,&sn,&serve1,&serve2);
    for (int i = 0; i < e; i++){
        int a,b;
        scanf("%d%d",&a,&b);
        adjlist[a].push_back(b);
    }
    dfs(s0);
    //printf("%d %d\n",dist(s0,serve1),dist(s0,serve2));
    int dist0 = max(dist(s0,serve1),dist(s0,serve2));
    int distn = min(dist(sn,serve1),dist(sn,serve2));
    //printf("%d %d\n",dist0,distn);
    if (dist0 > distn){
        printf("-1");
    }
    else printf("%d",distn-dist0);


}
/*
6 6
1 2 3 4
1 5
5 6
6 3
6 4
1 2
3 4

6 7
5 6 1 2
6 3
1 2
1 3
2 3
1 5
2 4
5 4
*/

Compilation message (stderr)

007.cpp: In function 'int lca(int, int)':
007.cpp:38:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
007.cpp: In function 'int main()':
007.cpp:49:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     int n,e; scanf("%d%d",&n,&e);
              ~~~~~^~~~~~~~~~~~~~
007.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d%d",&s0,&sn,&serve1,&serve2);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&a,&b);
         ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...