Submission #1766366


Source Code Expand

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <iomanip>
using namespace std;
  
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
typedef long long ll;
 
struct LowLink {
    
    vector < vector < int > > G;
    vector < int > ord, low;
    vector < bool > used;
    vector < pair < int, int > > bridge;
    vector < int > articulation_point;
    const int INF = 1145141919;
    
    LowLink (int n) : G(n), ord(n), low(n), used(n) {}
    
    void add_edge(int a, int b) {
        G[a].push_back(b);
        G[b].push_back(a);
    }
    
    void build(int root) {
        fill(low.begin(), low.end(), INF);
        fill(used.begin(), used.end(), false);
        dfs(root, -1, 1);
    }
    
    int dfs(int v, int prev, int cnt) {
        used[v] = true;
        low[v] = ord[v] = cnt;
        int min_low = cnt;
        int route = 0;
        bool isArticulation = false;
        for (int i = 0; i < G[v].size(); i++) {
            if (!used[G[v][i]]) {
                route++;
                int low_nxt = dfs(G[v][i], v, cnt + 1);
                if (ord[v] < low_nxt) {
                    bridge.push_back(make_pair(v, G[v][i]));
                }
                if (prev != -1 && G[v].size() >= 2 && low_nxt >= cnt) {
                    isArticulation = true;
                }
                min_low = min(min_low, low_nxt);
            } else if (G[v][i] != prev) {
                min_low = min(min_low, low[G[v][i]]);
            }
        }
        if (prev == -1 && route > 1) isArticulation = true;
        if (isArticulation) articulation_point.push_back(v);
        return low[v] = min_low;
    }
    
};

struct UnionFind {
    
    vector < int > node;
    UnionFind(int n) : node(n, -1) {}
    
    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x != y) {
            if (node[y] < node[x]) swap(x, y);
            node[x] += node[y];
            node[y] = x;
        }
        return x != y;
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    int root(int x) {
        return node[x] < 0 ? x : node[x] = root(node[x]);
    }
    
    int size(int x) {
        return -node[root(x)];
    }
    
};
 
struct LCA {
    
    int N,LOG_N;
    vector< vector< int > > parent;
    vector< vector< int > > G;
    vector< int > depth;
    
    LCA (int n) : N(n), depth(n), G(n), LOG_N((int)(log2(n)+1)) {
        parent = vector< vector< int > >(LOG_N, vector<int>(N));
    }
    
    void add_edge(int a, int b) {
        G[a].push_back(b);
        G[b].push_back(a);
    }
    
    void init(int root) {
        dfs(root, -1, 0);
        for (int k = 0; k + 1 < LOG_N; k++) {
            for (int v = 0; v < N; v++) {
                if (parent[k][v] < 0) parent[k+1][v] = -1;
                else parent[k+1][v] = parent[k][parent[k][v]];
            }
        }
    }
    
    void dfs(int v, int p, int d) {
        parent[0][v] = p;
        depth[v] = d;
        for (int i = 0; i < G[v].size(); i++) {
            if (G[v][i] != p) dfs(G[v][i], v, d+1);
        }
    }
    
    int dist(int a, int b) {
        int par = getParent(a,b);
        return depth[a] + depth[b] - depth[par] * 2;
    }
    
    int getParent(int u, int v) {
        if (depth[u] > depth[v]) swap(u,v);
        for (int k = 0; k < LOG_N; k++) {
            if (((depth[v] - depth[u]) >> k) & 1) v = parent[k][v];
        }
        if (u == v) return u;
        for (int k = LOG_N - 1; k >= 0; k--) {
            if(parent[k][u] != parent[k][v]){
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    
};
 
 
int N,M;
vector < pair < int,int > > edge;
set < pair < int, int > > bridge;
LowLink inst(100010);
UnionFind UF(100010);
LCA lca(100010);
 
int main() {
    
    cin >> N >> M;
    REP(i,M) {
        int a,b; cin >> a >> b;
        inst.add_edge(a, b);
        edge.push_back(make_pair(a,b));
    }
    
    inst.build(1);
    REP(i,inst.bridge.size()) {
        pair < int,int > e = inst.bridge[i];
        bridge.insert(e);
        swap(e.first, e.second);
        bridge.insert(e);
    }
    
    REP(i,M) {
        if (bridge.find(edge[i]) == bridge.end()) {
            UF.unite(edge[i].first, edge[i].second);
        }
    }
    
    int r = 0;
    REP(i,M) {
        int a = UF.root(edge[i].first);
        int b = UF.root(edge[i].second);
        if (a != b) {
            lca.add_edge(a, b);
            r = a;
        }
    }
    
    lca.init(r);
    
    int Q; cin >> Q;
    REP(kai,Q) {
        int A,B,C; cin >> A >> B >> C;
        A = UF.root(A);
        B = UF.root(B);
        C = UF.root(C);
        int d1 = lca.dist(A, C);
        int d2 = lca.dist(A, B) + lca.dist(B, C);
        if (d1 == d2) {
            cout << "OK" << endl;
        } else {
            cout << "NG" << endl;
        }
    }
    
    return 0;
}

Submission Info

Submission Time
Task D - 旅行会社高橋君
User kosakkun
Language C++14 (GCC 5.4.1)
Score 100
Code Size 5600 Byte
Status AC
Exec Time 616 ms
Memory 37492 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 80
Set Name Test Cases
Sample sample_0.txt, sample_1.txt, sample_2.txt
All bigcycleandsub_0.txt, bigcycleandsub_1.txt, cluster_0.txt, cluster_1.txt, cluster_2.txt, cluster_3.txt, cluster_4.txt, cycleline_0.txt, cycleline_1.txt, cycleline_2.txt, cycleline_3.txt, cycleline_4.txt, cycletree_0.txt, cycletree_1.txt, cycletree_2.txt, cycletree_3.txt, cycletree_4.txt, large_0.txt, large_1.txt, large_2.txt, line_0.txt, line_1.txt, lineandsub_0.txt, lineandsub_1.txt, lineandsub_2.txt, maxrand_0.txt, maxrand_1.txt, maxrand_2.txt, maxrandandsub_0.txt, maxrandandsub_1.txt, maxrandandsub_2.txt, maxrandandsub_3.txt, maxrandandsub_4.txt, med_0.txt, med_1.txt, med_2.txt, med_3.txt, med_4.txt, med_5.txt, med_6.txt, med_7.txt, med_8.txt, med_9.txt, perfect_0.txt, perfect_1.txt, perfect_2.txt, sample_0.txt, sample_1.txt, sample_2.txt, small_0.txt, small_1.txt, small_2.txt, small_3.txt, small_4.txt, small_5.txt, small_6.txt, small_7.txt, small_8.txt, small_9.txt, star_0.txt, star_1.txt, star_2.txt, tencluster_0.txt, tencluster_1.txt, tencluster_2.txt, tencluster_3.txt, tencluster_4.txt, tree_0.txt, tree_1.txt, tree_2.txt, verysmall_0.txt, verysmall_1.txt, verysmall_2.txt, verysmall_3.txt, verysmall_4.txt, verysmall_5.txt, verysmall_6.txt, verysmall_7.txt, verysmall_8.txt, verysmall_9.txt
Case Name Status Exec Time Memory
bigcycleandsub_0.txt AC 369 ms 24948 KB
bigcycleandsub_1.txt AC 370 ms 24948 KB
cluster_0.txt AC 443 ms 19824 KB
cluster_1.txt AC 430 ms 19824 KB
cluster_2.txt AC 430 ms 19696 KB
cluster_3.txt AC 430 ms 19824 KB
cluster_4.txt AC 425 ms 19696 KB
cycleline_0.txt AC 447 ms 18676 KB
cycleline_1.txt AC 442 ms 20724 KB
cycleline_2.txt AC 444 ms 18548 KB
cycleline_3.txt AC 446 ms 18548 KB
cycleline_4.txt AC 444 ms 18676 KB
cycletree_0.txt AC 479 ms 18420 KB
cycletree_1.txt AC 477 ms 18420 KB
cycletree_2.txt AC 474 ms 18420 KB
cycletree_3.txt AC 478 ms 18420 KB
cycletree_4.txt AC 478 ms 18420 KB
large_0.txt AC 354 ms 24048 KB
large_1.txt AC 86 ms 15096 KB
large_2.txt AC 254 ms 22896 KB
line_0.txt AC 421 ms 37492 KB
line_1.txt AC 251 ms 35828 KB
lineandsub_0.txt AC 616 ms 35700 KB
lineandsub_1.txt AC 613 ms 36980 KB
lineandsub_2.txt AC 592 ms 36340 KB
maxrand_0.txt AC 424 ms 26096 KB
maxrand_1.txt AC 427 ms 26096 KB
maxrand_2.txt AC 420 ms 26096 KB
maxrandandsub_0.txt AC 461 ms 26480 KB
maxrandandsub_1.txt AC 460 ms 26480 KB
maxrandandsub_2.txt AC 460 ms 26480 KB
maxrandandsub_3.txt AC 468 ms 26480 KB
maxrandandsub_4.txt AC 464 ms 26480 KB
med_0.txt AC 13 ms 13568 KB
med_1.txt AC 11 ms 13568 KB
med_2.txt AC 11 ms 13568 KB
med_3.txt AC 12 ms 13568 KB
med_4.txt AC 13 ms 13568 KB
med_5.txt AC 12 ms 13568 KB
med_6.txt AC 13 ms 13568 KB
med_7.txt AC 12 ms 13568 KB
med_8.txt AC 10 ms 13568 KB
med_9.txt AC 11 ms 13568 KB
perfect_0.txt AC 162 ms 14840 KB
perfect_1.txt AC 70 ms 14840 KB
perfect_2.txt AC 160 ms 14964 KB
sample_0.txt AC 10 ms 13568 KB
sample_1.txt AC 10 ms 13568 KB
sample_2.txt AC 10 ms 13568 KB
small_0.txt AC 12 ms 13568 KB
small_1.txt AC 11 ms 13568 KB
small_2.txt AC 10 ms 13568 KB
small_3.txt AC 10 ms 13568 KB
small_4.txt AC 12 ms 13568 KB
small_5.txt AC 12 ms 13568 KB
small_6.txt AC 10 ms 13568 KB
small_7.txt AC 12 ms 13568 KB
small_8.txt AC 12 ms 13568 KB
small_9.txt AC 11 ms 13568 KB
star_0.txt AC 506 ms 31476 KB
star_1.txt AC 502 ms 31476 KB
star_2.txt AC 504 ms 31476 KB
tencluster_0.txt AC 430 ms 20592 KB
tencluster_1.txt AC 426 ms 20336 KB
tencluster_2.txt AC 426 ms 20208 KB
tencluster_3.txt AC 423 ms 20720 KB
tencluster_4.txt AC 428 ms 20720 KB
tree_0.txt AC 587 ms 31220 KB
tree_1.txt AC 585 ms 31220 KB
tree_2.txt AC 587 ms 31220 KB
verysmall_0.txt AC 10 ms 13568 KB
verysmall_1.txt AC 10 ms 13568 KB
verysmall_2.txt AC 10 ms 13568 KB
verysmall_3.txt AC 10 ms 13568 KB
verysmall_4.txt AC 10 ms 13568 KB
verysmall_5.txt AC 10 ms 13568 KB
verysmall_6.txt AC 10 ms 13568 KB
verysmall_7.txt AC 10 ms 13568 KB
verysmall_8.txt AC 10 ms 13568 KB
verysmall_9.txt AC 10 ms 13568 KB