Submission #982299
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<bool> vb;
typedef pair<int,int> edge;
vector<vi> G;
void bridge_dfs(int now, int from, vector<edge> &bridge, vector<vi> &bicomp, stack<int> &roots, stack<int> &S, vb &inS, vi &preord, int &pre_ct){
preord[now] = pre_ct++;
S.push(now); inS[now]=true;
roots.push(now);
rep(i,G[now].size()){
int to = G[now][i];
if(preord[to] == -1) bridge_dfs(to,now,bridge,bicomp,roots,S,inS,preord,pre_ct);
else if(from != to && inS[to]) while(preord[roots.top()] > preord[to]) roots.pop();
}
if(now == roots.top()){
bridge.pb(edge(now,from));
bicomp.pb(vi());
while(1){
int v=S.top(); S.pop(); inS[v]=false;
bicomp.back().pb(v);
if(now == v) break;
}
roots.pop();
}
}
void bridge_detect(vector<edge> &bridge, vector<vi> &bicomp){
const int n=G.size();
vi preord(n,-1);
vb inS(n,false);
stack<int> roots,S;
int pre_ct=0;
rep(i,n)if(preord[i] == -1){
bridge_dfs(i,n,bridge,bicomp,roots,S,inS,preord,pre_ct);
bridge.pop_back();
}
}
typedef pair<int,int> pi;
const int INF=12345678;
//ノードの個数
const int MAX_V =100000;
//ダブリングに必要なサイズ(log(MAX_V))
const int MAX_LOG_V = 18;
//木の隣接リスト表現
vector<int> bG[MAX_V];
//根のノード番号
int root = 0;
//親を2^k回辿って到達するノード(根を通り過ぎる場合,-1)
int parent[MAX_LOG_V][MAX_V];
//根からの深さ
int depth[MAX_V];
//現在vに注目、親はp、深さd
void lca_dfs(int v, int p, int d){
parent[0][v]=p;
depth[v]=d;
rep(i,bG[v].size()){
if(bG[v][i]!=p){ //親でなければ子
lca_dfs(bG[v][i], v, d+1);
}
}
}
//初期化
void lca_init(int V){
//parent[0]とdepthの初期化
lca_dfs(root, -1, 0);
//parentの初期化
for(int k=0; k+1<MAX_LOG_V; ++k){
for(int v=0; v<V; ++v){
if(parent[k][v] < 0) parent[k+1][v]=-1;
else parent[k+1][v] = parent[k][parent[k][v]];
}
}
}
//uとvのLCAを求める
int lca(int u, int v){
//uとvの深さが同じになるまで親を辿る
if(depth[u] > depth[v]) swap(u,v);
for(int k=0; k<MAX_LOG_V; ++k){
if((depth[v]-depth[u])>>k & 1) v = parent[k][v];
}
if(u==v) return u;
//二分探索でLCAを求める
for(int k=MAX_LOG_V-1; k>=0; --k){
if(parent[k][u] != parent[k][v]){
u=parent[k][u];
v=parent[k][v];
}
}
return parent[0][u];
}
int d[100000];
int calc(int x, int y)
{
return d[x]+d[y]-2*d[lca(x,y)];
}
int main()
{
int n,m;
scanf(" %d %d", &n, &m);
G = vector<vi>(n);
rep(i,m)
{
int x,y;
scanf(" %d %d", &x, &y);
--x; --y;
G[x].pb(y); G[y].pb(x);
}
vector<edge> bridge;
vector<vi> bicomp;
bridge_detect(bridge,bicomp);
int B=bicomp.size();
vector<int> gr(n);
rep(i,B)rep(j,bicomp[i].size()) gr[bicomp[i][j]]=i;
rep(i,bridge.size())
{
int a=gr[bridge[i].fi], b=gr[bridge[i].se];
bG[a].pb(b); bG[b].pb(a);
}
priority_queue<pi,vector<pi>,greater<pi>> que;
// dijkstra
fill(d,d+B,INF);
d[0]=0;
que.push(pi(0,0));
while(!que.empty())
{
pi p=que.top();
que.pop();
int v=p.se;
if(d[v]<p.fi) continue;
rep(i,bG[v].size())
{
int to=bG[v][i];
if(d[to]>d[v]+1)
{
d[to]=d[v]+1;
que.push(pi(d[to],to));
}
}
}
lca_init(B);
int Q;
scanf(" %d", &Q);
while(Q--)
{
int a,b,c;
scanf(" %d %d %d", &a, &b, &c);
a = gr[a-1];
b = gr[b-1];
c = gr[c-1];
if(calc(a,c) == calc(a,b)+calc(b,c)) printf("OK\n");
else printf("NG\n");
}
return 0;
}
Submission Info
Submission Time |
|
Task |
D - 旅行会社高橋君 |
User |
imulan |
Language |
C++11 (GCC 4.9.2) |
Score |
100 |
Code Size |
4435 Byte |
Status |
AC |
Exec Time |
264 ms |
Memory |
42428 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:122:28: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf(" %d %d", &n, &m);
^
./Main.cpp:128:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf(" %d %d", &x, &y);
^
./Main.cpp:173:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf(" %d", &Q);
^
./Main.cpp:177:39: 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 |
|
|
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, 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 |
166 ms |
26960 KB |
bigcycleandsub_1.txt |
AC |
166 ms |
26960 KB |
cluster_0.txt |
AC |
243 ms |
11812 KB |
cluster_1.txt |
AC |
169 ms |
12052 KB |
cluster_2.txt |
AC |
165 ms |
11628 KB |
cluster_3.txt |
AC |
167 ms |
11928 KB |
cluster_4.txt |
AC |
168 ms |
11676 KB |
cycleline_0.txt |
AC |
162 ms |
11540 KB |
cycleline_1.txt |
AC |
160 ms |
11424 KB |
cycleline_2.txt |
AC |
160 ms |
11244 KB |
cycleline_3.txt |
AC |
161 ms |
11284 KB |
cycleline_4.txt |
AC |
159 ms |
11540 KB |
cycletree_0.txt |
AC |
158 ms |
10648 KB |
cycletree_1.txt |
AC |
158 ms |
10652 KB |
cycletree_2.txt |
AC |
161 ms |
10652 KB |
cycletree_3.txt |
AC |
159 ms |
10648 KB |
cycletree_4.txt |
AC |
158 ms |
10652 KB |
large_0.txt |
AC |
162 ms |
23328 KB |
large_1.txt |
AC |
48 ms |
6004 KB |
large_2.txt |
AC |
125 ms |
23256 KB |
line_0.txt |
AC |
208 ms |
42428 KB |
line_1.txt |
AC |
140 ms |
38720 KB |
lineandsub_0.txt |
AC |
257 ms |
37572 KB |
lineandsub_1.txt |
AC |
264 ms |
40896 KB |
lineandsub_2.txt |
AC |
258 ms |
39360 KB |
maxrand_0.txt |
AC |
190 ms |
28436 KB |
maxrand_1.txt |
AC |
188 ms |
28304 KB |
maxrand_2.txt |
AC |
188 ms |
28424 KB |
maxrandandsub_0.txt |
AC |
192 ms |
27728 KB |
maxrandandsub_1.txt |
AC |
191 ms |
27600 KB |
maxrandandsub_2.txt |
AC |
192 ms |
27600 KB |
maxrandandsub_3.txt |
AC |
191 ms |
27596 KB |
maxrandandsub_4.txt |
AC |
193 ms |
27732 KB |
med_0.txt |
AC |
20 ms |
3276 KB |
med_1.txt |
AC |
21 ms |
3228 KB |
med_2.txt |
AC |
20 ms |
3356 KB |
med_3.txt |
AC |
21 ms |
3360 KB |
med_4.txt |
AC |
22 ms |
3488 KB |
med_5.txt |
AC |
22 ms |
3348 KB |
med_6.txt |
AC |
22 ms |
3488 KB |
med_7.txt |
AC |
21 ms |
3480 KB |
med_8.txt |
AC |
20 ms |
3356 KB |
med_9.txt |
AC |
21 ms |
3356 KB |
perfect_0.txt |
AC |
67 ms |
4076 KB |
perfect_1.txt |
AC |
42 ms |
4128 KB |
perfect_2.txt |
AC |
65 ms |
4132 KB |
sample_0.txt |
AC |
21 ms |
3228 KB |
sample_1.txt |
AC |
19 ms |
3232 KB |
sample_2.txt |
AC |
21 ms |
3232 KB |
small_0.txt |
AC |
20 ms |
3148 KB |
small_1.txt |
AC |
21 ms |
3356 KB |
small_2.txt |
AC |
21 ms |
3228 KB |
small_3.txt |
AC |
20 ms |
3348 KB |
small_4.txt |
AC |
21 ms |
3232 KB |
small_5.txt |
AC |
21 ms |
3344 KB |
small_6.txt |
AC |
21 ms |
3228 KB |
small_7.txt |
AC |
21 ms |
3104 KB |
small_8.txt |
AC |
20 ms |
3100 KB |
small_9.txt |
AC |
21 ms |
3352 KB |
star_0.txt |
AC |
209 ms |
28668 KB |
star_1.txt |
AC |
209 ms |
28672 KB |
star_2.txt |
AC |
208 ms |
28676 KB |
tencluster_0.txt |
AC |
170 ms |
13976 KB |
tencluster_1.txt |
AC |
168 ms |
13292 KB |
tencluster_2.txt |
AC |
167 ms |
12956 KB |
tencluster_3.txt |
AC |
171 ms |
14360 KB |
tencluster_4.txt |
AC |
170 ms |
14188 KB |
tree_0.txt |
AC |
243 ms |
26560 KB |
tree_1.txt |
AC |
243 ms |
26556 KB |
tree_2.txt |
AC |
243 ms |
26560 KB |
verysmall_0.txt |
AC |
19 ms |
3228 KB |
verysmall_1.txt |
AC |
21 ms |
3104 KB |
verysmall_2.txt |
AC |
19 ms |
3352 KB |
verysmall_3.txt |
AC |
19 ms |
3348 KB |
verysmall_4.txt |
AC |
21 ms |
3228 KB |
verysmall_5.txt |
AC |
21 ms |
3232 KB |
verysmall_6.txt |
AC |
19 ms |
3228 KB |
verysmall_7.txt |
AC |
21 ms |
3356 KB |
verysmall_8.txt |
AC |
20 ms |
3224 KB |
verysmall_9.txt |
AC |
20 ms |
3220 KB |