답안 #71872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
71872 2018-08-25T18:04:30 Z bnahmad15 One-Way Streets (CEOI17_oneway) C++17
100 / 100
579 ms 59692 KB
#include <bits/stdc++.h>
#define forn(i,j,n) for(int i = (int)j;i<=(int)n;i++)
#define nfor(i,j,n) for(int i = (int)j;i>=(int)n;i--)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 100001,LOG = 17;

int n,m,p,u,v,x[N],y[N],is[N],cnt = 0,num[N],low[N],det[N],cc = 0,comp[N],vis[N],nxt[N],pr[N][LOG],dep[N];
pair<int,int> st[N];
vector<pair<int,pii> > g[N],g2[N];
map <pii,int> mp;
char res[N];

inline pii rev(pii a){
	if(a.second == 1)
		a.second = 2;
	else if(a.second == 2)
		a.second = 1;
	return a;
}
void dfs(int uu,int prnt){
	low[uu] = num[uu] = ++cnt;
	forn(i,0,g[uu].size()-1){
		int vv = g[uu][i].first;
		if(num[vv] == -1){
			dfs(vv,uu);
			if(low[vv] > num[uu]){
				is[g[uu][i].second.first] = 1;
			}
			low[uu] = min(low[uu],low[vv]);
		}else if(vv != prnt){
			low[uu] = min(low[uu],num[vv]);
		}
	}
}

void DFS(int node,int numb){
	if(vis[node] == 1)
		return;
	vis[node] = 1;
	comp[node] = numb;
	forn(i,0,g[node].size()-1){
		int to = g[node][i].first;
		if(vis[to])
			continue;
		if(is[g[node][i].second.first] == 1){
			continue;
		}else{
			DFS(to,numb);
		}
	}
}

void dfs2(int node,int prnt,int depth,pii state){
	pr[node][0] = prnt;
	dep[node] = depth;
	st[node] = rev(state);
	forn(i,0,g2[node].size()-1){
		if(g2[node][i].first == prnt)
			continue;
		dfs2(g2[node][i].first,node,depth+1,g2[node][i].second);
	}
}
int LCA(int a,int b){
	if(dep[a] < dep[b])
		swap(a,b);
	nfor(i,LOG-1,0){
		if(dep[a] - (1<<i) >= dep[b])
			a = pr[a][i];
	}
	if(a == b)
		return a;
	nfor(i,LOG-1,0){
		if(pr[a][i] != pr[b][i]){
			a = pr[a][i];
			b = pr[b][i];
		}
	}
	return pr[a][0];
}
int FIND(int a){return ((nxt[a] == a) ? a : nxt[a] = FIND(nxt[a]));}
void UNION(int a,int b){
	int fa = FIND(a);
	int fb = FIND(b);
	nxt[fa] = fb;
}
void calc(int a,int b,int dir){
	if(dep[a] <= dep[b])
		return;
	int fa = FIND(a);
	if(a == fa){
		if(dir == 1)
			det[st[a].first] = st[a].second;
		else{
			pii tmp = rev(st[a]);
			det[tmp.first] = tmp.second;
		}
		UNION(a,FIND(pr[a][0]));
		calc(pr[a][0],b,dir);
	}else{
		calc(fa,b,dir);
	}
}
int main(){

	scanf("%d%d",&n,&m);
	forn(i,1,m){
		scanf("%d%d",&u,&v);
		g[u].push_back(make_pair(v,make_pair(i,1)));
		g[v].push_back(make_pair(u,make_pair(i,2)));
		if(u > v)
			swap(u,v);
		mp[make_pair(u,v)]++;
		is[i] = 0;
	}
	scanf("%d",&p);
	forn(i,1,p){
		scanf("%d%d",&x[i],&y[i]);
	}
	forn(i,1,n){
		num[i] = low[i] = -1;
		vis[i] = 0;
	}
	forn(i,1,n){
		if(num[i] == -1)
			dfs(i,0);
	}
	forn(i,1,n){
		forn(j,0,g[i].size()-1){
			u = i,v = g[i][j].first;
			if(u > v)
				swap(u,v);
			if(mp[make_pair(u,v)] > 1)
				is[g[i][j].second.first] = 0;
			if(u == v)
				is[g[i][j].second.first] = 0;
		}
	}
	forn(i,1,n){
		dep[i] = -1;
		if(vis[i] == true)
			continue;
		DFS(i,++cc);
	}
	forn(i,1,n){
		forn(j,0,g[i].size()-1){
			u = i,v = g[i][j].first;
			if(comp[u] == comp[v])
				continue;
			g2[comp[u]].push_back(make_pair(comp[v],g[i][j].second));
		}
		nxt[i] = i;
	}
	forn(i,1,m){
		det[i] = 0;
		if(is[i] == 1)
			continue;
		res[i] = 'B';
	}
	forn(i,1,cc){
		if(dep[i] != -1)
			continue;

		dfs2(i,0,0,make_pair(0,0));
	}
	forn(i,1,LOG-1){
		forn(j,1,cc){
			pr[j][i] = pr[pr[j][i-1]][i-1];
		}
	}
	forn(i,1,p){
		if(comp[x[i]] == comp[y[i]])
			continue;
		int C = LCA(comp[x[i]],comp[y[i]]);
		calc(comp[x[i]],C,1);
		calc(comp[y[i]],C,2);
	}
	forn(i,1,m){
		if(is[i] == 0)
			continue;
		if(det[i] == 1)
			res[i] = 'R';
		else if(det[i] == 2)
			res[i] = 'L';
		else
			res[i] = 'B';
	}
	forn(i,1,m)
		printf("%c",res[i]);
	return 0;
}

Compilation message

oneway.cpp: In function 'int main()':
oneway.cpp:107:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&m);
  ~~~~~^~~~~~~~~~~~~~
oneway.cpp:109:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&u,&v);
   ~~~~~^~~~~~~~~~~~~~
oneway.cpp:117:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&p);
  ~~~~~^~~~~~~~~
oneway.cpp:119:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&x[i],&y[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 8 ms 5224 KB Output is correct
3 Correct 8 ms 5464 KB Output is correct
4 Correct 8 ms 5580 KB Output is correct
5 Correct 11 ms 5764 KB Output is correct
6 Correct 9 ms 5764 KB Output is correct
7 Correct 10 ms 5792 KB Output is correct
8 Correct 9 ms 5792 KB Output is correct
9 Correct 9 ms 5792 KB Output is correct
10 Correct 8 ms 5792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 8 ms 5224 KB Output is correct
3 Correct 8 ms 5464 KB Output is correct
4 Correct 8 ms 5580 KB Output is correct
5 Correct 11 ms 5764 KB Output is correct
6 Correct 9 ms 5764 KB Output is correct
7 Correct 10 ms 5792 KB Output is correct
8 Correct 9 ms 5792 KB Output is correct
9 Correct 9 ms 5792 KB Output is correct
10 Correct 8 ms 5792 KB Output is correct
11 Correct 279 ms 19324 KB Output is correct
12 Correct 263 ms 21340 KB Output is correct
13 Correct 320 ms 24080 KB Output is correct
14 Correct 338 ms 28188 KB Output is correct
15 Correct 343 ms 30716 KB Output is correct
16 Correct 417 ms 37796 KB Output is correct
17 Correct 390 ms 40092 KB Output is correct
18 Correct 419 ms 40092 KB Output is correct
19 Correct 338 ms 43428 KB Output is correct
20 Correct 310 ms 43428 KB Output is correct
21 Correct 225 ms 43428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 8 ms 5224 KB Output is correct
3 Correct 8 ms 5464 KB Output is correct
4 Correct 8 ms 5580 KB Output is correct
5 Correct 11 ms 5764 KB Output is correct
6 Correct 9 ms 5764 KB Output is correct
7 Correct 10 ms 5792 KB Output is correct
8 Correct 9 ms 5792 KB Output is correct
9 Correct 9 ms 5792 KB Output is correct
10 Correct 8 ms 5792 KB Output is correct
11 Correct 279 ms 19324 KB Output is correct
12 Correct 263 ms 21340 KB Output is correct
13 Correct 320 ms 24080 KB Output is correct
14 Correct 338 ms 28188 KB Output is correct
15 Correct 343 ms 30716 KB Output is correct
16 Correct 417 ms 37796 KB Output is correct
17 Correct 390 ms 40092 KB Output is correct
18 Correct 419 ms 40092 KB Output is correct
19 Correct 338 ms 43428 KB Output is correct
20 Correct 310 ms 43428 KB Output is correct
21 Correct 225 ms 43428 KB Output is correct
22 Correct 486 ms 47808 KB Output is correct
23 Correct 478 ms 48640 KB Output is correct
24 Correct 438 ms 51284 KB Output is correct
25 Correct 539 ms 57664 KB Output is correct
26 Correct 453 ms 57664 KB Output is correct
27 Correct 579 ms 58072 KB Output is correct
28 Correct 101 ms 58072 KB Output is correct
29 Correct 292 ms 58072 KB Output is correct
30 Correct 318 ms 58072 KB Output is correct
31 Correct 372 ms 58072 KB Output is correct
32 Correct 304 ms 59692 KB Output is correct