Portal:http://codeforces.com/problemset/problem/506/B

    http://codeforces.com/problemset/problem/505/D 

好题

给n个城市,m条有向边,求出最少的有向边使得其构成的图与原图等势

对于每个连通分量:

如果无环,那么只需要需要n-1条边完成联通

如果有环,则只需要n条边完成联通

所以这题只要判下连通分量,再看有几个连通分量有环即可

解法一:无向图遍历求强连通分量再把强连通分量所代表的联通分量dfs判环,如下

Memory: 10440 KB   Time: 498 MS
 #include<iostream>
#include<algorithm>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define FORD(i,j,k) for(int i=j;i>=k;i--)
#define LL long long
#define SZ(x) int(x.size())
#define maxn 100010
int n,m,x,y;
bool circle=true;
int vis[maxn],vis2[maxn];
vector<int> neg[maxn],adj[maxn];
vector<int> dot;
void ndfs(int start)
{
dot.push_back(start);
vis[start]=;
FOR(i,,SZ(neg[start])-)
{
if(!vis[neg[start][i]]) ndfs(neg[start][i]); }
return;
}
void adfs(int start)
{
vis2[start]=;
FOR(i,,SZ(adj[start])-)
{
if(!vis2[adj[start][i]]) adfs(adj[start][i]);
else if(vis2[adj[start][i]]==)circle=false;
}
vis2[start]=;
return;
}
int main()
{
cin>>n>>m;
FOR(i,,m)
{
cin>>x>>y;
neg[x].push_back(y);
neg[y].push_back(x);
adj[x].push_back(y);
}
int ans=n;
FOR(i,,n)
{
circle=;
if(!vis[i])
{
dot.clear();
ndfs(i);
FOR(i,,SZ(dot)-)
if (!vis2[dot[i]]) adfs(dot[i]);
ans-=circle;
}
}
cout<<ans<<endl;
return ;
}

我在想函数名字到底取afs好还是adfs好

解法二:在无向图中维护并查集求强连通分量再把强连通分量所代表的联通分量用拓扑排序判环,如下

Memory: 7820 KB   Time: 514 MS
 #include<iostream>
#include<algorithm>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define FORD(i,j,k) for(int i=j;i>=k;i--)
#define LL long long
#define SZ(x) int(x.size())
#define maxn 100010
vector<int> G[maxn];
int n,m,x,y,T;
int father[maxn],val[maxn],circle[maxn],vis[maxn],clock[maxn];
int setfind(int x)
{
int fa=father[x];
if(fa==x) return x;
else return father[x]=setfind(fa);
}
void setunion(int x,int y)
{
int X=setfind(x);
int Y=setfind(y);
if(X==Y) return;
if(val[X]>val[Y]) father[Y]=X;
else father[X]=Y;
if(val[X]==val[Y]) val[X]++;
return;
}
void dfs(int start)
{
vis[start]=;
FOR(i,,SZ(G[start])-)
if(!vis[G[start][i]]) dfs(G[start][i]);
clock[start]=++T;
return;
}
int main()
{
cin>>n>>m;
FOR(i,,n)
{father[i]=i;val[i]=;}
FOR(i,,m)
{
cin>>x>>y;
G[x].push_back(y);
setunion(x,y);
}
FOR(i,,n)
if(!vis[i]) dfs(i);
FOR(i,,n)
FOR(j,,SZ(G[i])-)
if(clock[i]<clock[G[i][j]]) circle[setfind(i)]=;
int ans=n;
FOR(i,,n)
if(i==setfind(i))if(!circle[setfind(i)]) ans--;
cout<<ans<<endl;
return ;
}

Mr. Kitayuta's Black Technology

其实求连通分量还可以用染色

  判有向环可以用并查集乱搞

反正就是怎么搞都能过

CodeForces 506B/505D Mr. Kitayuta's Technology的更多相关文章

  1. codeforces 505C C. Mr. Kitayuta, the Treasure Hunter(dp)

    题目链接: C. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 me ...

  2. 【codeforces 505D】Mr. Kitayuta's Technology

    [题目链接]:http://codeforces.com/problemset/problem/505/D [题意] 让你构造一张有向图; n个点; 以及所要求的m对联通关系(xi,yi) 即要求这张 ...

  3. [CF#286 Div2 D]Mr. Kitayuta's Technology(结论题)

    题目:http://codeforces.com/contest/505/problem/D 题目大意:就是给你一个n个点的图,然后你要在图中加入尽量少的有向边,满足所有要求(x,y),即从x可以走到 ...

  4. 【CF505D】Mr. Kitayuta's Technology

    题目大意: 在一个有向图中,有n个顶点,给出m对数字(u,v)表示顶点u和顶点v必须直接或者间接相连,让你构造一个这样的图,输出最少需要多少条边. 挖坑待填 官方题解链接:http://codefor ...

  5. codeforce 505 D. Mr. Kitayuta's Technology(tarjan+并查集)

    题目链接:http://codeforces.com/contest/505/problem/D 题解:先用tarjan缩点然后再用并查集注意下面这种情况 ‘ 这种情况只需要构成一个大环就行了,也就是 ...

  6. 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter

    [题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...

  7. Codeforces 505 A Mr. Kitayuta's Gift【暴力】

    题意:给出一个字符串,可以向该字符串的任意位置插入一个字母使其变成回文串 因为字符串的长度小于10,枚举插入的字母,和要插入的位置,再判断是否已经满足回文串 #include<iostream& ...

  8. 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化

    神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...

  9. CodeForces 505B Mr. Kitayuta's Colorful Graph

    Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

随机推荐

  1. JZOJ 3526. 【NOIP2013模拟11.7A组】不等式(solve)

    3526. [NOIP2013模拟11.7A组]不等式(solve) (File IO): input:solve.in output:solve.out Time Limits: 1000 ms M ...

  2. Flask 使用pycharm 创建项目,一个简单的web 搭建

    1:新建项目后 2:Flask web 项目重要的就是app 所有每个都需要app app=Flask(__name__)   3:Flask 的路径是有app.route('path')装饰决定, ...

  3. Keil MDK版兼容51系列单片机开发环境安装

    一.安装源文件下载 百度网盘链接:https://pan.baidu.com/s/18tnjFgVat4q2hDSh7LAD8A 提取码:    2295 二.安装及破解 1.安装51的编辑器 双击安 ...

  4. Oracle数据库使用sysdba登陆时出现ORA-01031: insufficient privileges问题

    今天在自己本本上装上了oracle数据库,然而在命令框登录时 用 sqlplus / as sysdba   时却出现了: insufficient privileges问题 原因就是没有加入ora_ ...

  5. 聊一聊React中虚拟DOM

    1. 什么是虚拟 DOM 在 React 中实际上是 render 函数中return 的内容会生成 DOM,return 中的内容由两部分组成,一部分是 JSX ,另一部分就是 state 中的数据 ...

  6. 2016 Multi-University Training Contest 1 T4

    http://acm.hdu.edu.cn/showproblem.php?pid=5726 求不修改区间gcd可以用线段树或者倍增. 求l-n的我们注意观察gcd(a​l​​,a​l+1​​,... ...

  7. 【opencv系列02】OpenCV4.X图像读取与显示

    一.读取图片 opencv中采用imread() 函数读取图像 imread(filename, flags=None)     filename 图片的路径     flags 图像读取方式 ● c ...

  8. JDBC封装-Java(新手)

    JDBC的封装,自己总结的自己总结的自己总结的 dao (代码分层)命名规范: 1.com.XXX.dao 存放dao相关的类型 例如 StudentDAOImpl 处理 数据库的链接 存取数据 2. ...

  9. scapy学习笔记

    1.ACK Scan >>>ans,unans=sr(IP(dst="www.baidu.com")/TCP(dport=[80,666],flags=" ...

  10. hdu1253胜利大逃亡(城堡的怪物太狠,主角难免天天逃亡)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1253/ 其实就是二维扩展到三维了,增加了搜索方向,其他的没什么不同. 代码如下: #include<bit ...