题目地址:http://codeforces.com/contest/506/problem/B

先用强连通判环。然后转化成无向图,找无向图连通块。若一个有n个点的块内有强连通环,那么须要n条边。即正好首尾相连形成一条环,那么有了这个环之后,在这个块内的全部要求都能实现。

假设没有强连通环,那么就是一棵树,那么仅仅须要n-1条边就可以。

代码例如以下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
#include <time.h>
using namespace std;
#define LL long long
#define pi acos(-1.0)
#pragma comment(linker, "/STACK:1024000000")
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=100000+10;
int head[MAXN], cnt;
int dfn[MAXN], low[MAXN], scc, belong[MAXN], instk[MAXN], stk[MAXN], indx, tot[MAXN], top;
int vis[MAXN];
int flag, num;
struct node
{
int u, v, next;
}edge[MAXN<<1];
void add(int u, int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void init()
{
memset(head,-1,sizeof(head));
cnt=indx=top=0;
memset(dfn,0,sizeof(dfn));
memset(instk,0,sizeof(instk));
memset(tot,0,sizeof(tot));
}
void tarjan(int u)
{
dfn[u]=low[u]=++indx;
stk[++top]=u;
instk[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instk[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
scc++;
while(1){
int v=stk[top--];
belong[v]=scc;
instk[v]=0;
tot[scc]++;
if(u==v) break;
}
}
}
void dfs(int u)
{
num++;
vis[u]=1;
if(tot[belong[u]]>=2) flag=1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(vis[v]) continue ;
dfs(v);
}
}
int main()
{
int n, m, u, v, i, j, ans;
while(scanf("%d%d",&n,&m)!=EOF){
init();
while(m--){
scanf("%d%d",&u,&v);
add(u,v);
}
ans=0;
for(i=1;i<=n;i++){
if(!dfn[i])
tarjan(i);
}
m=cnt;
for(i=0;i<m;i++){
u=edge[i].u;
v=edge[i].v;
add(v,u);
}
memset(vis,0,sizeof(vis));
ans=0;
for(i=1;i<=n;i++){
if(!vis[i]){
num=flag=0;
dfs(i);
ans+=num-1+flag;
}
}
printf("%d\n",ans);
}
return 0;
}

Codeforces Round #286 (Div. 1) B. Mr. Kitayuta&#39;s Technology (强连通分量)的更多相关文章

  1. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  2. 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift

    题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...

  3. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集

    D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...

  4. Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs

    B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...

  5. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph

    D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...

  6. Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP

    题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...

  7. Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)

    数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...

  8. Codeforces Round #286 (Div. 2)A. Mr. Kitayuta's Gift(暴力,string的应用)

    由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...

  9. CodeForces Round #286 Div.2

    A. Mr. Kitayuta's Gift (枚举) 题意: 给一个长度不超过10的串,问能否通过插入一个字符使得新串成为回文串. 分析: 因为所给的串很多,所以可以枚举 “在哪插入” 和 “插入什 ...

随机推荐

  1. python 学习笔记 12 -- 写一个脚本获取城市天气信息

    近期在玩树莓派,前面写过一篇在树莓派上使用1602液晶显示屏,那么可以显示后最重要的就是显示什么的问题了. 最easy想到的就是显示时间啊,CPU利用率啊.IP地址之类的.那么我认为呢,假设可以显示当 ...

  2. MongoDB 2.6安装

    Workaround to install as a service You can manually install 2.6.0 as a service on Windows from an Ad ...

  3. POJ 2353 DP

    双向DP+记录路径. // by SiriusRen #include <stack> #include <cstdio> #include <cstring> u ...

  4. mybatis的sql语句导致索引失效,使得查询超时

    mybaitis书写sql需要特别注意where条件中的语句,否则将会导致索引失效,使得查询总是超时.如下语句会出现导致索引失效的情况: with test1 as (select count(C_F ...

  5. swift使用查阅资料备份1

    SnapKit RxSwift R.swift https://www.jianshu.com/p/68e12b966d86 iOS - RxSwift 项目实战记录 https://blog.csd ...

  6. Core Graphics框架 利用Quartz 2D绘图

    首先,什么是Core Graphics和Quartz 2D? Core Graphics:是基于Quartz 2D绘图引擎的一个C语言的API绘图框架.它也是iOS开发中最基本的框架(Framewor ...

  7. Java学习进阶—高级编程

    当你已经熟练的掌握了面向对象中的各种概念后,是否会对这些知识是如何使用的产生浓厚的兴趣?本课程主要针对于已经掌握了JAVA核心开发技术的读者准备,讲解了JAVA多线程.常用类库.IO编程.网络编程.类 ...

  8. Day 01 计算机编程基础

    1.编程语言是什么? 编程语言是人与计算机交流的介质 2.什么是编程? 用编程语言写出一个个文件,这堆文件会达到一个目的 3.编程有什么用? 让计算机帮助我们干活,从而解放人类劳动力 4.计算机组成原 ...

  9. JavaFX 的 UI 控件集 ControlsFX

    出处:http://www.oschina.net/p/controlsfx JavaFX 的 UI 控件集 ControlsFX ControlsFX 开源项目旨在为 JavaFX 开发提供更多的 ...

  10. d3基础图形模板笔记

    散点图(scatter plot): http://bl.ocks.org/weiglemc/6185069 雷达图(radar): http://xgfe.github.io/uploads/che ...