codeforces 659E . New Reform 强连通
题目链接
对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是0。 否则对答案贡献是1.
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5+5;
int num, head[maxn], s[maxn], ok[maxn], dfn[maxn], low[maxn];
int instack[maxn], st[maxn], vis[maxn], deep, cnt, top;
struct node
{
int to, nextt;
}e[maxn*2];
void add(int u, int v) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++deep;
st[++top] = u;
instack[u] = 1;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
if(!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
} else if(instack[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u]) {
++cnt;
int v;
do {
v = st[top--];
instack[v] = 0;
s[v] = cnt;
} while(u != v);
}
}
int bfs(int u) {
queue <int> q;
q.push(u);
vis[u] = 1;
int flag = 0;
while(!q.empty()) {
u = q.front(); q.pop();
if(ok[u])
flag = 1;
for(int i = head[u]; ~i ; i = e[i].nextt) {
int v = e[i].to;
if(vis[v])
continue;
q.push(v);
vis[v] = 1;
}
}
return !flag;
}
pll ed[maxn];
int main()
{
int n, m;
cin>>n>>m;
mem1(head);
for(int i = 0; i < m; i++) {
scanf("%d%d", &ed[i].fi, &ed[i].se);
add(ed[i].fi, ed[i].se);
add(ed[i].se, ed[i].fi);
}
for(int i = 1; i <= n; i++)
if(!dfn[i])
tarjan(i, 0);
num = 0;
mem1(head);
for(int i = 0; i < m; i++) {
int u = s[ed[i].fi], v = s[ed[i].se];
if(u == v) {
ok[u] = 1;
continue;
}
add(u, v);
add(v, u);
}
int ans = 0;
for(int i = 1; i <= cnt; i++) {
if(!vis[i]) {
ans += bfs(i);
}
}
cout<<ans<<endl;
return 0;
}
codeforces 659E . New Reform 强连通的更多相关文章
- CodeForces 659E New Reform
题意:给你一个无向图,如今要求你把边改成有向的. 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论.假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了 ...
- Codeforces 659E New Reform【DFS】
题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...
- CodeForces 659E New Reform (图的遍历判环)
Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...
- [图中找环] Codeforces 659E New Reform
New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- codeforces 659E E. New Reform(图论)
题目链接: E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 732F. Tourist Reform (Tarjan缩点)
题目链接:http://codeforces.com/problemset/problem/732/F 题意: 给出一个有n个点m条边的无向图,保证联通,现在要求将所有边给定一个方向使其变成有向图,设 ...
- Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)
<题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...
- CodeForces 732F Tourist Reform
边双连通分量. 这题有一点构造的味道.一个有向图,经过强连通缩点之后会形成一个有向无环图. 如果将最大的强连通分量放在顶端,其余的强连通分量都直接或间接指向他,那么这样就构造出了符合要求的图. 接下来 ...
- CodeForces 723E One-Way Reform
构造. 有一种十分巧妙的方法可以使图中所有度数为偶数的节点,经过每条边定向后,出度和入度都相等. 首先统计每个节点的度数,将度数为奇数的节点与编号为$n+1$的节点连边,这样一来,这张新图变成了每个节 ...
随机推荐
- asp.net实现UNIX 时间戳
//1 将系统时间转换成UNIX时间戳 DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1 ...
- 等待事件:enq: HW - contention和enq: TM - contention
今天生成了生产库前几日的AWR报告,发现等待事件中出现了一个陌生的event--enq: HW - contention,google一下是ASSM(Auto Segment Space Manage ...
- UINavigationController和UITabBarController合用
一.创建一个 Tabbed Application.默认创建的是带有两个Tab的工程. 二.在AppDelegate.h里面添加 @property (strong, nonatomic) UINav ...
- @property属性
1. 读写属性(readwrite/ readonly) 默认为readwrite,表示该属性既可以读取,也可以给该属性变量赋值:readonly则表示只能读取该属性变量. 2. 原子属性 (atom ...
- JS 精粹(方法)
数组方法: 模拟队列的操作:push()/shift();unshift()/pop();模拟栈操作:push()/pop(); push()返回增加后的长度.unshift也是.pop和shift返 ...
- 最长增长子序列 DP
#include<iostream> using namespace std; #define INF 0x7fffffff #define N 10000 // O(n^2) int l ...
- Java 中类与类之间的关系
在java中类和类之间的关系基本上有依赖.关联.聚合.组合等 一.继承关系 继承指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父接口)的功能,并可以增加它自己的新功能的能力.在J ...
- PostgreSql入门命令
1 命令行登录数据库 有两种方式,一是直接在系统shell下执行psql命令:而是先进入psql环境,然后再连接数据库.下面分别给出实例: (1)直接登录 执行命令:psql -h 192.168.1 ...
- Flex中如何通过设置GridLines对象的horizontalAlternateFill样式交错显示LineSeries图表背景颜色的例子
原文 http://blog.minidx.com/2008/11/27/1652.html 接下来的例子演示了Flex中如何通过设置GridLines对象的horizontalAlternateFi ...
- (Pre sell) ZOPO ZP998 (C2 II) 5.5 inch smart phone True Octa Core MTK6592 1920X1080 FHD screen 401 ppi 2GB/32GB 14.0Mp camera-in Mobile Phones from Electronics on Aliexpress.com
(Pre sell) ZOPO ZP998 (C2 II) 5.5 inch smart phone True Octa Core MTK6592 1920X1080 FHD screen 401 p ...