Submission #1756481


Source Code Expand

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M, Q;
vector<int> G[100000], G2[100000], G3[100000];

bool used[100000];
int T[100000], ord[100000], R[100000];
int U[20][100000];

void dfs(int x, int p, int r) {
  used[x] = true;
  R[x] = r;
  for (int t : G[x]) {
    if (t == p) continue;
    if (used[t]) {
      if (R[t] > R[x]) {
        T[t]++;
        T[x]--;
      }
      continue;
    }
    dfs(t, x, r+1);
  }
}

int dfs2(int x, int p) {
  used[x] = true;
  int s = T[x];
  for (int t : G[x]) {
    if (t == p) continue;
    if (used[t]) continue;
    s += dfs2(t, x);
  }
  if (s) G2[x].pb(p), G2[p].pb(x);
  return s;
}

void dfs3(int x, int p, int k) {
  ord[x] = k;
  for (int t : G2[x]) {
    if (t == p) continue;
    dfs3(t, x, k);
  }
}
void dfs4(int x, int p, int r) {
  R[x] = r;
  U[0][x] = p;
  for (int t : G3[x]) {
    if (t == p) continue;
    dfs4(t, x, r+1);
  }
}

int lca(int x, int y) {
  if (R[x] < R[y]) swap(x, y);
  for (int k=19; k>=0; k--) if (R[x]-R[y] >= 1<<k) x = U[k][x];
  if (x == y) return x;
  for (int k=19; k>=0; k--) if (U[k][x] != U[k][y]) x = U[k][x], y = U[k][y];
  return U[0][x];
}

int dist(int x, int y) {
  return R[x] + R[y] - 2*R[lca(x, y)];
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N >> M;
  rep(i, M) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].pb(v);
    G[v].pb(u);
  }
  dfs(0, -1, 0);
  rep(i, N) used[i] = false;
  dfs2(0, -1);

  rep(i, N) ord[i] = -1;
  int k = 0;
  rep(i, N) if (ord[i] == -1) dfs3(i, -1, k++);
  rep(x, N) {
    for (int t : G[x]) {
      if (ord[x] == ord[t]) continue;
      G3[ord[x]].pb(ord[t]);
      G3[ord[t]].pb(ord[x]);
    }
  }
  rep(x, k) sort(all(G3[x])), uniq(G3[x]);
  dfs4(0, -1, 0);
  rep(k, 19) rep(i, N) {
    if (U[k][i] == -1) U[k+1][i] = -1;
    else U[k+1][i] = U[k][U[k][i]];
  }

  cin >> Q;
  rep(_, Q) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--, c--;
    a = ord[a], b = ord[b], c = ord[c];
    if (dist(a, b) + dist(b, c) == dist(a, c)) cout << "OK\n";
    else cout << "NG\n";
  }
  return 0;
}

Submission Info

Submission Time
Task D - 旅行会社高橋君
User funcsr
Language C++14 (GCC 5.4.1)
Score 100
Code Size 2740 Byte
Status AC
Exec Time 203 ms
Memory 29440 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 104 ms 28460 KB
bigcycleandsub_1.txt AC 100 ms 28288 KB
cluster_0.txt AC 135 ms 23936 KB
cluster_1.txt AC 139 ms 24064 KB
cluster_2.txt AC 138 ms 23808 KB
cluster_3.txt AC 138 ms 24064 KB
cluster_4.txt AC 136 ms 23936 KB
cycleline_0.txt AC 138 ms 23040 KB
cycleline_1.txt AC 149 ms 23040 KB
cycleline_2.txt AC 150 ms 23040 KB
cycleline_3.txt AC 162 ms 23040 KB
cycleline_4.txt AC 141 ms 23168 KB
cycletree_0.txt AC 139 ms 22784 KB
cycletree_1.txt AC 135 ms 22784 KB
cycletree_2.txt AC 132 ms 22784 KB
cycletree_3.txt AC 128 ms 22784 KB
cycletree_4.txt AC 127 ms 22784 KB
large_0.txt AC 123 ms 26624 KB
large_1.txt AC 24 ms 16128 KB
large_2.txt AC 92 ms 26240 KB
line_0.txt AC 145 ms 28800 KB
line_1.txt AC 99 ms 27264 KB
lineandsub_0.txt AC 203 ms 27264 KB
lineandsub_1.txt AC 184 ms 28544 KB
lineandsub_2.txt AC 194 ms 27904 KB
maxrand_0.txt AC 138 ms 29312 KB
maxrand_1.txt AC 141 ms 29440 KB
maxrand_2.txt AC 138 ms 29440 KB
maxrandandsub_0.txt AC 140 ms 29056 KB
maxrandandsub_1.txt AC 143 ms 29056 KB
maxrandandsub_2.txt AC 140 ms 29056 KB
maxrandandsub_3.txt AC 142 ms 29056 KB
maxrandandsub_4.txt AC 141 ms 29056 KB
med_0.txt AC 6 ms 14464 KB
med_1.txt AC 5 ms 14336 KB
med_2.txt AC 6 ms 14464 KB
med_3.txt AC 6 ms 14464 KB
med_4.txt AC 6 ms 14464 KB
med_5.txt AC 6 ms 14464 KB
med_6.txt AC 6 ms 14464 KB
med_7.txt AC 6 ms 14464 KB
med_8.txt AC 5 ms 14336 KB
med_9.txt AC 6 ms 14464 KB
perfect_0.txt AC 36 ms 15360 KB
perfect_1.txt AC 22 ms 15360 KB
perfect_2.txt AC 35 ms 15360 KB
sample_0.txt AC 5 ms 14336 KB
sample_1.txt AC 5 ms 14336 KB
sample_2.txt AC 5 ms 14336 KB
small_0.txt AC 6 ms 14336 KB
small_1.txt AC 5 ms 14336 KB
small_2.txt AC 5 ms 14336 KB
small_3.txt AC 5 ms 14336 KB
small_4.txt AC 6 ms 14336 KB
small_5.txt AC 6 ms 14336 KB
small_6.txt AC 5 ms 14336 KB
small_7.txt AC 5 ms 14336 KB
small_8.txt AC 6 ms 14336 KB
small_9.txt AC 5 ms 14336 KB
star_0.txt AC 128 ms 24052 KB
star_1.txt AC 119 ms 24052 KB
star_2.txt AC 121 ms 24052 KB
tencluster_0.txt AC 144 ms 24704 KB
tencluster_1.txt AC 142 ms 24448 KB
tencluster_2.txt AC 141 ms 24320 KB
tencluster_3.txt AC 151 ms 24832 KB
tencluster_4.txt AC 141 ms 24832 KB
tree_0.txt AC 161 ms 23808 KB
tree_1.txt AC 163 ms 23680 KB
tree_2.txt AC 161 ms 23808 KB
verysmall_0.txt AC 5 ms 14336 KB
verysmall_1.txt AC 5 ms 14336 KB
verysmall_2.txt AC 6 ms 14336 KB
verysmall_3.txt AC 5 ms 14336 KB
verysmall_4.txt AC 5 ms 14336 KB
verysmall_5.txt AC 5 ms 14336 KB
verysmall_6.txt AC 5 ms 14336 KB
verysmall_7.txt AC 5 ms 14336 KB
verysmall_8.txt AC 5 ms 14336 KB
verysmall_9.txt AC 5 ms 14336 KB