题目链接

对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是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 强连通的更多相关文章

  1. CodeForces 659E New Reform

    题意:给你一个无向图,如今要求你把边改成有向的. 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论.假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了 ...

  2. Codeforces 659E New Reform【DFS】

    题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...

  3. CodeForces 659E New Reform (图的遍历判环)

    Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...

  4. [图中找环] Codeforces 659E New Reform

    New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  5. codeforces 659E E. New Reform(图论)

    题目链接: E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces 732F. Tourist Reform (Tarjan缩点)

    题目链接:http://codeforces.com/problemset/problem/732/F 题意: 给出一个有n个点m条边的无向图,保证联通,现在要求将所有边给定一个方向使其变成有向图,设 ...

  7. Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)

    <题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...

  8. CodeForces 732F Tourist Reform

    边双连通分量. 这题有一点构造的味道.一个有向图,经过强连通缩点之后会形成一个有向无环图. 如果将最大的强连通分量放在顶端,其余的强连通分量都直接或间接指向他,那么这样就构造出了符合要求的图. 接下来 ...

  9. CodeForces 723E One-Way Reform

    构造. 有一种十分巧妙的方法可以使图中所有度数为偶数的节点,经过每条边定向后,出度和入度都相等. 首先统计每个节点的度数,将度数为奇数的节点与编号为$n+1$的节点连边,这样一来,这张新图变成了每个节 ...

随机推荐

  1. SWFUpload的使用及其注意事项

    SWFUpload的使用: 添加Jquery    swfuploaad.js  handler.js文件 配置参数: upload_url:文件将要被传到的处理程序 post_params:{“”: ...

  2. GridView专栏

    鉴于GridView的强大,鄙人突然心血来潮,想把GridView单独拿出来整理一下. (一)gridview如何加自增长列 protected void GridView1_RowDataBound ...

  3. Ext布局篇

      EXT标准布局类 收藏 面板相当于一张干净的白纸,如果直接在上面添加内容,将很难控制面板中内容的显示位置,面板元素越多就越显得凌乱,所以需要在面板上划分不同的区域,将面板内容展示到希望的位置上.E ...

  4. Mschart应用之曲线图表spline

    本文主要是Mschart应用之曲线图表spline,实现6个模拟数据的图表,其中数据源X轴为当前系统时间,Y轴是由随机函数产生的不同范围的随机数. 首先是自定义一个数据表,然后产生的数据添加到该数据表 ...

  5. 由chkconfig 引发的联想——怎么查看程序是否已经安装/成功安装

    由chkconfig 引发的联想--怎么查看程序是否已经安装/成功安装 某天需要运行chkconfig,root登录依然找不到该命令. [root@localhost ~]# chkconfig ba ...

  6. CSS3中轻松实现渐变效果

    background: -moz-linear-gradient(top, #8fa1ff, #3757fa); /* Firefox */ background: -webkit-gradient( ...

  7. 自学HTML5第三节(拖放效果)

    今天来看看网页上的拖放效果,首先来看看什么是拖放———— 拖放 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 浏览器支持 Inte ...

  8. java中等于和equals的区别

    面试的时候没答上,很受打击,特写在这里. ==是判断两个变量或实例是不是指向同一个内存空间equals是判断两个变量或实例所指向的内存空间的值是不是相同 除了String和封装器,equals()和“ ...

  9. 识别Json字符串并分隔成Map集合

    识别Json字符串并分隔成Map集合 前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新 ...

  10. 修改默认的undo_retention参数设置

    昨天,一个朋友的数据库数据被误操作删除掉了,请求我帮忙进行恢复. 数据库版本是Oracle10g Release 2的,我首先想到的是使用Flashback Query进行闪回恢复,不幸的是ORA-0 ...