CodeForces 555E Case of Computer Network

790人阅读 评论(0)

n（2*10^5）个点m（2*10^5）条边的无向图  要求给无向边定向  使得最后的有向图满足q（2*10^5）个指令  每个指令表示为s->e  即s到e有通路  问  是否有可能

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
using namespace std;
typedef long long LL;
#define N 200010

int n, m, q;

struct edge {
int u, v, flag, next;
} ed[N << 1];

int dfn[N], low[N], idx, block, sec, stack[N], top;
int vis[N], hsh[N], tree[N];
int lca[N][20], dis[N];
int up[N], down[N];

void add(int u, int v) {
ed[tot].u = u;
ed[tot].v = v;
ed[tot].flag = 0;
}

void tarjan(int u) {
dfn[u] = low[u] = ++idx;
stack[++top] = u;
tree[u] = sec;
for (int i = head[u]; ~i; i = ed[i].next) {
int v = ed[i].v;
if (ed[i].flag) continue;
ed[i].flag = ed[i^1].flag = 1;
if (dfn[v] == -1) {
tarjan(v);
low[u] = min(low[u], low[v]);
if (dfn[u] < low[v]) ed[i].flag = ed[i^1].flag = -1;
} else low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
block++;
int v;
do {
v = stack[top--];
hsh[v] = block;
} while (u != v);
}
}

void dfs(int u, int c) {
hsh[u] = c;
for (int i = head[u]; ~i; i = ed[i].next) {
if (ed[i].flag == -1) continue;
int v = ed[i].v;
if (hsh[v] == -1) dfs(v, c);
}
}

void init(int u, int from) {
vis[u] = 1;
dis[u] = dis[from] + 1;
lca[u][0] = from;
for (int i = 1; i < 20; i++) {
lca[u][i] = lca[lca[u][i - 1]][i - 1];
}
for (int i = head[u]; ~i; i = ed[i].next) {
int v = ed[i].v;
if (v != from) init(v, u);
}
}

int get(int u, int v) {
if (dis[v] > dis[u]) swap(u, v);
int i, tmp = dis[u] - dis[v];
for (i = 19; tmp; i--) {
if (tmp >= (1 << i)) {
tmp -= (1 << i);
u = lca[u][i];
}
}
if (u == v) return u;
for (i = 19; i >= 0; i--) {
if (lca[u][i] != lca[v][i]) {
u = lca[u][i];
v = lca[v][i];
}
}
return lca[u][0];
}

bool ok(int u) {
vis[u] = 0;
for (int i = head[u]; ~i; i = ed[i].next) {
int v = ed[i].v;
if (vis[v]) {
if (!ok(v)) return false;
up[u] += up[v];
down[u] += down[v];
}
}
return !(up[u] && down[u]);
}

int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
}
memset(dfn, -1, sizeof (dfn));
for (int i = 1; i <= n; i++) {
if (dfn[i] == -1) {
sec++;
tarjan(i);
}
}
int tmp = tot;
tot = 0;
for (int i = 0; i < tmp; i++) {
if (ed[i].flag == -1 && hsh[ed[i].u] != hsh[ed[i].v]) {
}
}
n = block;
for (int i = 1; i <= n; i++) {
if (!vis[i]) init(i, i);
}
bool ans = true;
for (int i = 1; i <= q; i++) {
int u, v;
scanf("%d%d", &u, &v);
if (tree[u] != tree[v]) {
puts("No");
return 0;
}
u = hsh[u];
v = hsh[v];
if (u != v) {
int fa = get(u, v);
up[u]++;
up[fa]--;
down[v]++;
down[fa]--;
}
}
for (int i = 1; i <= n; i++) {
if (vis[i]) {
ans = ok(i);
if (!ans) break;
}
}
if (ans) puts("Yes");
else puts("No");
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：188428次
• 积分：4664
• 等级：
• 排名：第7254名
• 原创：285篇
• 转载：1篇
• 译文：0篇
• 评论：44条
文章分类
阅读排行
最新评论