답안 #197447

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
197447 2020-01-21T09:28:59 Z quocnguyen1012 One-Way Streets (CEOI17_oneway) C++14
100 / 100
363 ms 34860 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define Task "ONEWAY"

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 5;

int nTime=0;
vector <int> adj[maxn]; 
int Visited[maxn], Low[maxn], Parent[maxn]; 
int U[maxn], V[maxn];
int N, M, P;
int st[maxn], en[maxn];
bool Bridge[maxn];

bool minimize(int &a, int b){ 
	if (a>b) a=b; 
	else return false; 
	return true; 
}

void visit(int u){
	///cerr << u << ' ' << Parent[u] << '\n';
    Low[u] = Visited[u] = ++nTime;
    for (auto it : adj[u]){
		int v = U[it] + V[it] - u;
		if (it!=Parent[u]){
			if (Visited[v]) 
				minimize(Low[u], Visited[v]);
			else {
				Parent[v]=it;
				visit(v); 
				minimize(Low[u], Low[v]);
				if (Low[v]>=Visited[v]) 
					Bridge[it] = 1;
			}
		}
	}
}

bool vis[maxn];
int comp[maxn];
int SCC = 0;

void dfs (int u){
	vis[u] = 1;
	comp[u] = SCC;
	for (auto it : adj[u]){
		if (Bridge[it]) continue;
		int v = U[it] + V[it] - u;
		if (!vis[v]) dfs (v);
	}
}

vector <pair <int, int>> G[maxn];
int p[maxn][21];
int cnt[maxn];
int lca(int x,int y){
    if (cnt[x]<cnt[y])
         swap (x,y);
    for(int k=20;k>=0;--k)
    {
        if (cnt[p[x][k]]>=cnt[y])
        {
            x=p[x][k];
        }
    }
    if (x==y)
        return x;
    for (int k=20;k>=0;--k)
    {
        if (p[x][k]!=p[y][k])
        {
            x=p[x][k];
            y=p[y][k];
        }
    }
    return p[x][0];
    //cerr<<
}

pair <int, int> par[maxn];

void findcnt (int u, int P){
	for (auto it : G[u]){
		if (it.fi != P){
			cnt[it.fi] = cnt[u] + 1;
			p[it.fi][0] = u;
			par[it.fi] = mp (u, it.se);
			findcnt (it.fi, u);
		}
	}
}

void Init_LCA (void){
	for (int i=1; i<=SCC; ++i){
		if (cnt[i] == 0) cnt[i] = 1, findcnt (i, 0);
	}
	for (int k=1; (1<<k)<=SCC; k++)
		for (int i=1; i<=SCC; ++i)
			p[i][k] = p[p[i][k-1]][k-1];
}

char res[maxn];

void query (int u, int v, int direct){
	int p = par[u].fi, id = par[u].se;
	if (u == v) return;
	if (res[id] != '?') return;
	if (direct == 1){
		if (comp[U[id]] == u) res[id] = 'R';
		else res[id] = 'L';
	}
	else {
		if (comp[U[id]] == u) res[id] = 'L';
		else res[id] = 'R';
	}
	query (p, v, direct);
}

struct Query{
	int st, en, direct;
	bool operator < (const Query & other) const {
		return st < other.st;
	}
};

signed main (void){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if (fopen (Task".INP", "r")){
		freopen (Task".INP", "r", stdin);
		freopen (Task".OUT", "w", stdout);
	}
	cin >> N >> M;
	for (int i=1; i<=M; ++i){
		cin >> U[i] >> V[i];
		adj[U[i]].pb (i); adj[V[i]].pb (i);
	}
	cin >> P;
	for (int i=1; i<=P; ++i)
		cin >> st[i] >> en[i];
	for (int i=1; i<=N; ++i){
		if (Visited[i] == 0){
			visit(i);
		}
	}
	for (int i=1; i<=N; ++i){
		if (vis[i] == 0){
			++SCC;
			dfs (i);
		}
	}
	for (int i=1; i<=M; ++i){
		if (Bridge[i]){
			G[comp[U[i]]].pb (mp (comp[V[i]], i));
			G[comp[V[i]]].pb (mp (comp[U[i]], i));
		}
	}
	Init_LCA();
	fill (begin(res), end(res), '?');
	///for (int i=1; i<=N; ++i) cerr << comp[i] << '\n';
	vector <pair <int, Query>> go;
	for (int i=1; i<=P; ++i){
		st[i] = comp[st[i]], en[i] = comp[en[i]];
		int lc = lca (st[i], en[i]);
		///query (st[i], lc, 1);
		///query (en[i], lc, -1);
		Query rnd = {st[i], lc, 1};
		go.pb (mp (cnt[lc], rnd));
		rnd.st = en[i], rnd.direct = -1;
		go.pb (mp (cnt[lc], rnd));
	}
	sort (go.begin(), go.end());
	for (auto all : go){
		query (all.se.st, all.se.en, all.se.direct);
	}
	for (int i=1; i<=M; ++i){
		if (res[i] == '?') cout << 'B';
		else cout << res[i];
	}
}

Compilation message

oneway.cpp: In function 'int main()':
oneway.cpp:137:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen (Task".INP", "r", stdin);
   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:138:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen (Task".OUT", "w", stdout);
   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5240 KB Output is correct
2 Correct 7 ms 5240 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 7 ms 5368 KB Output is correct
5 Correct 6 ms 5496 KB Output is correct
6 Correct 7 ms 5368 KB Output is correct
7 Correct 7 ms 5368 KB Output is correct
8 Correct 7 ms 5368 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 7 ms 5240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5240 KB Output is correct
2 Correct 7 ms 5240 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 7 ms 5368 KB Output is correct
5 Correct 6 ms 5496 KB Output is correct
6 Correct 7 ms 5368 KB Output is correct
7 Correct 7 ms 5368 KB Output is correct
8 Correct 7 ms 5368 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 7 ms 5240 KB Output is correct
11 Correct 58 ms 9948 KB Output is correct
12 Correct 69 ms 11128 KB Output is correct
13 Correct 91 ms 12536 KB Output is correct
14 Correct 108 ms 16532 KB Output is correct
15 Correct 117 ms 18036 KB Output is correct
16 Correct 143 ms 25336 KB Output is correct
17 Correct 135 ms 26488 KB Output is correct
18 Correct 146 ms 25332 KB Output is correct
19 Correct 187 ms 27472 KB Output is correct
20 Correct 72 ms 11000 KB Output is correct
21 Correct 66 ms 11000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5240 KB Output is correct
2 Correct 7 ms 5240 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 7 ms 5368 KB Output is correct
5 Correct 6 ms 5496 KB Output is correct
6 Correct 7 ms 5368 KB Output is correct
7 Correct 7 ms 5368 KB Output is correct
8 Correct 7 ms 5368 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 7 ms 5240 KB Output is correct
11 Correct 58 ms 9948 KB Output is correct
12 Correct 69 ms 11128 KB Output is correct
13 Correct 91 ms 12536 KB Output is correct
14 Correct 108 ms 16532 KB Output is correct
15 Correct 117 ms 18036 KB Output is correct
16 Correct 143 ms 25336 KB Output is correct
17 Correct 135 ms 26488 KB Output is correct
18 Correct 146 ms 25332 KB Output is correct
19 Correct 187 ms 27472 KB Output is correct
20 Correct 72 ms 11000 KB Output is correct
21 Correct 66 ms 11000 KB Output is correct
22 Correct 363 ms 32596 KB Output is correct
23 Correct 272 ms 31300 KB Output is correct
24 Correct 268 ms 31464 KB Output is correct
25 Correct 296 ms 34860 KB Output is correct
26 Correct 280 ms 32260 KB Output is correct
27 Correct 263 ms 31464 KB Output is correct
28 Correct 74 ms 13288 KB Output is correct
29 Correct 124 ms 16712 KB Output is correct
30 Correct 127 ms 16996 KB Output is correct
31 Correct 126 ms 17084 KB Output is correct
32 Correct 186 ms 22756 KB Output is correct