Submission #1349748


Source Code Expand

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010


int bridges_dfs(int now, int back, vector<vector<pair<int,int> > > &way,
                vector<int> &visited, vector<int> &sum, vector<bool> &bridges_flag){
  int res_sum = 0;
  
  visited[now] = 1;
  
  for(int i=0;i<way[now].size();i++){
    if(way[now][i].first == back) continue;

    if(visited[way[now][i].first] == 1){
      sum[way[now][i].first]--;
      sum[now]++;
      bridges_flag[way[now][i].second] = false;
    }else if(visited[way[now][i].first] == 0){
      int res = bridges_dfs(way[now][i].first, now, way, visited, sum, bridges_flag);
      res_sum += res;
      if(res > 0){
        bridges_flag[way[now][i].second] = false;
      }
    }
  }

  visited[now] = 2;
  return res_sum + sum[now];
}

vector<bool> bridges(int n, vector<pair<int,int> > path){ //無向辺
  vector<vector<pair<int,int> > >  way(n, vector<pair<int,int> >());
  vector<int> visited(n,0);
  vector<int> sum(n,0);
  vector<bool> bridges_flag(path.size(), true);

  for(int i=0;i<path.size();i++){
    way[path[i].first].push_back({path[i].second,i});
    way[path[i].second].push_back({path[i].first,i});
  }

  bridges_dfs(0, -1, way, visited, sum, bridges_flag);

  return bridges_flag;
}

/* UnionFind */

struct UnionFind{
  vector<int> data, tree_size;
  UnionFind(int s):data(s,-1),tree_size(s,1) {}
  
  int root(int x){
    if(data[x]==-1) return x;
    return data[x]=root(data[x]);
  }
  
  bool set(int x,int y){
    x=root(x);
    y=root(y);
    if(x==y) return false;
    data[y]=x;
    tree_size[x] += tree_size[y];
    tree_size[y] = 0;
    return true;
  }
  
  bool check(int x,int y){
    x=root(x);
    y=root(y);
    return x==y;
  }
  
  int size(int x){
    return tree_size[root(x)];
  }
  
};

/*Lowest Common Ancstor*/

struct LCA{
  vector<vector<int> > parent;
  vector<int> depth;
  int max_log;
  
  void dfs(vector<vector<int> > &tree,int now,int p,int d){
    parent[0][now] = p;
    depth[now] = d;
    
    for(int i=0;i<tree[now].size();i++){
      if(tree[now][i] != p)
        dfs(tree,tree[now][i],now,d+1);
    }
  }
  
  void init(vector<vector<int> > &tree,int n,int root){
    max_log = 0;
    
    for(int i=1;i<=n*2;i*=2){
      parent.push_back(vector<int>(n,-1));
      max_log++;
    }
    depth.assign(n,-1);
    
    dfs(tree,root,-1,0);
    
    for(int i=0;i<max_log-1;i++){
      for(int j=0;j<n;j++){
        if(parent[i][j]==-1)
          parent[i+1][j] = -1;
        else
          parent[i+1][j] = parent[i][parent[i][j]];
      }
    }
  }
  
  int query(int a,int b){
    if(depth[a] > depth[b]) swap(a,b);
    
    for(int i=0;i<max_log;i++){
      if((depth[b]-depth[a]) >> i & 1){
        b = parent[i][b];
      }
    }
    
    if(a==b) return a;
    
    for(int i=max_log-1;i>=0;i--){
      if(parent[i][a]!=parent[i][b]){
        a = parent[i][a];
        b = parent[i][b];
      }
    }
    
    return parent[0][a];
  }
  
};



int main(){
  int n,m,a,b;
  vector<pair<int,int> > way;

  scanf("%d%d",&n,&m);

  for(int i=0;i<m;i++){
    scanf("%d%d",&a,&b);
    a--; b--;
    way.push_back({a,b});
  }

  auto bridges_flag = bridges(n, way);
  UnionFind uf(n);
  vector<vector<int> > way2(n,vector<int>());
  int v = n;
  
  for(int i=0;i<m;i++){
    if(bridges_flag[i] == false){
      v -= uf.set(way[i].first, way[i].second);
    }
  }

  int root[SIZE];

  for(int i=0;i<n;i++) root[i] = uf.root(i);
  
  for(int i=0;i<m;i++){
    
    if(bridges_flag[i]){
      way2[root[way[i].first]].push_back(root[way[i].second]);
      way2[root[way[i].second]].push_back(root[way[i].first]);
    }
  }

  LCA lca;
  
  lca.init(way2, n, root[0]);

  int q;
  
  scanf("%d",&q);

  for(int i=0;i<q;i++){
    int a, b, c;

    scanf("%d%d%d",&a,&b,&c);
    a = root[a-1];
    b = root[b-1];
    c = root[c-1];

    int t = lca.query(a,c);
    
    if((lca.query(a,b) == b ||
       lca.query(c,b) == b) &&
       lca.depth[t] <= lca.depth[b]){
      puts("OK");
    }else{
      puts("NG");
    }
  }
  return 0;
}

Submission Info

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

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:174:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&n,&m);
                      ^
./Main.cpp:177:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&a,&b);
                        ^
./Main.cpp:211:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&q);
                 ^
./Main.cpp:216:29: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d",&a,&b,&c);
                             ^

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 86 ms 22460 KB
bigcycleandsub_1.txt AC 88 ms 22460 KB
cluster_0.txt AC 121 ms 13716 KB
cluster_1.txt AC 134 ms 13852 KB
cluster_2.txt AC 123 ms 13644 KB
cluster_3.txt AC 121 ms 13728 KB
cluster_4.txt AC 111 ms 13648 KB
cycleline_0.txt AC 156 ms 13200 KB
cycleline_1.txt AC 167 ms 13072 KB
cycleline_2.txt AC 186 ms 12944 KB
cycleline_3.txt AC 185 ms 12940 KB
cycleline_4.txt AC 154 ms 13196 KB
cycletree_0.txt AC 181 ms 12556 KB
cycletree_1.txt AC 182 ms 12556 KB
cycletree_2.txt AC 182 ms 12560 KB
cycletree_3.txt AC 182 ms 12556 KB
cycletree_4.txt AC 184 ms 12556 KB
large_0.txt AC 110 ms 20464 KB
large_1.txt AC 19 ms 3192 KB
large_2.txt AC 82 ms 19152 KB
line_0.txt AC 146 ms 25660 KB
line_1.txt AC 86 ms 23100 KB
lineandsub_0.txt AC 231 ms 22716 KB
lineandsub_1.txt AC 199 ms 24764 KB
lineandsub_2.txt AC 217 ms 23740 KB
maxrand_0.txt AC 127 ms 23980 KB
maxrand_1.txt AC 127 ms 23980 KB
maxrand_2.txt AC 129 ms 24048 KB
maxrandandsub_0.txt AC 129 ms 23212 KB
maxrandandsub_1.txt AC 129 ms 23212 KB
maxrandandsub_2.txt AC 129 ms 23212 KB
maxrandandsub_3.txt AC 129 ms 23212 KB
maxrandandsub_4.txt AC 123 ms 23212 KB
med_0.txt AC 2 ms 384 KB
med_1.txt AC 1 ms 256 KB
med_2.txt AC 2 ms 384 KB
med_3.txt AC 2 ms 384 KB
med_4.txt AC 2 ms 512 KB
med_5.txt AC 2 ms 384 KB
med_6.txt AC 2 ms 384 KB
med_7.txt AC 2 ms 384 KB
med_8.txt AC 1 ms 256 KB
med_9.txt AC 2 ms 384 KB
perfect_0.txt AC 31 ms 3316 KB
perfect_1.txt AC 22 ms 3700 KB
perfect_2.txt AC 31 ms 3444 KB
sample_0.txt AC 1 ms 256 KB
sample_1.txt AC 1 ms 256 KB
sample_2.txt AC 1 ms 256 KB
small_0.txt AC 1 ms 256 KB
small_1.txt AC 1 ms 256 KB
small_2.txt AC 1 ms 256 KB
small_3.txt AC 1 ms 256 KB
small_4.txt AC 1 ms 256 KB
small_5.txt AC 1 ms 256 KB
small_6.txt AC 1 ms 256 KB
small_7.txt AC 1 ms 256 KB
small_8.txt AC 1 ms 256 KB
small_9.txt AC 1 ms 256 KB
star_0.txt AC 151 ms 15788 KB
star_1.txt AC 148 ms 15788 KB
star_2.txt AC 144 ms 17836 KB
tencluster_0.txt AC 119 ms 15060 KB
tencluster_1.txt AC 119 ms 14580 KB
tencluster_2.txt AC 121 ms 14392 KB
tencluster_3.txt AC 120 ms 15264 KB
tencluster_4.txt AC 122 ms 15224 KB
tree_0.txt AC 216 ms 15548 KB
tree_1.txt AC 215 ms 15676 KB
tree_2.txt AC 216 ms 15548 KB
verysmall_0.txt AC 1 ms 256 KB
verysmall_1.txt AC 1 ms 256 KB
verysmall_2.txt AC 1 ms 256 KB
verysmall_3.txt AC 1 ms 256 KB
verysmall_4.txt AC 1 ms 256 KB
verysmall_5.txt AC 1 ms 256 KB
verysmall_6.txt AC 1 ms 256 KB
verysmall_7.txt AC 1 ms 256 KB
verysmall_8.txt AC 1 ms 256 KB
verysmall_9.txt AC 1 ms 256 KB