codefoces 22E 图论
有些题还得练练模拟的能力呀
这题一言难尽,他给的 图不是半连通子图呀,我崩溃了
要分好组,然后1给2连,2给3连,。。。。。n给1连。。
具体看代码,我写的比较捞了吧。。
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
const int maxn = 2e5 + 7;
vector<int>G[maxn], G2[maxn]; void insert(int be, int en) {
G[be].push_back(en);
}
stack<int>s;
int in[maxn];
int out[maxn];
int n;
int dfn[maxn], low[maxn], clor[maxn], ins[maxn], ans, df; int tarjan(int x) {
s.push(x);
dfn[x] = low[x] = ++df;
for (int i = 0; i < G[x].size(); i++) {
int p = G[x][i];
if (!dfn[p]) {
tarjan(p);
low[x] = min(low[x], low[p]);
}
else if (!clor[p]) {
low[x] = min(low[x], dfn[p]);
}
}
if (dfn[x] == low[x]) {
ans++;
while (1) {
int a = s.top();
s.pop();
clor[a] = ans;
ins[ans] = a;
if (a == x) break;
}
}
return 0;
}
int find() {
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
tarjan(i);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < G[i].size(); j++) {
int be = clor[i];
int en = clor[G[i][j]];
if (be != en) {
in[en]++;
out[be]++;//出
G2[be].push_back(en);
}
}
}
for (int i = 1; i <= n; i++) {
G[i].clear();
}
return 0;
}
int vis[maxn]; int dfs(int x) {
for (int i = 0; i < G2[x].size(); i++) {
int p = G2[x][i];
dfs(p);
vis[x] = vis[p];
}
return 0;
}
int list[maxn];
vector<int>cns;
int main() { scanf("%d", &n);
int x;
int y;
for (int i = 1; i <= n ; i++) {
scanf("%d", &x);
G[i].push_back(x); }
find();
if (ans == 1) {
printf("0\n");
return 0;
}
n = ans;
int c = 0;
for (int i = 1; i <= n; i++) {
if (out[i] == 0) {
vis[i] = ++c;
list[c] = i;
}
}
for (int i = 1; i <= n; i++) {
if (in[i] == 0) {
dfs(i);
}
} for (int i = 1; i <= n; i++) {
if (in[i] == 0) {
G[vis[i]].push_back(i);
}
}
for (int i = 1; i < c; i++) {
//list[i]--->G[i+1]
for (int j = 0; j < G[i + 1].size(); j++) {
cns.push_back(list[i]);
cns.push_back(G[i + 1][j]);
}
}
for (int i = 0; i < G[1].size(); i++) {
cns.push_back(list[c]);
cns.push_back(G[1][i]);
}
printf("%d\n", cns.size() / 2);
for (int i = 0; i < cns.size(); i+=2) {
printf("%d %d\n", ins[cns[i]], ins[cns[i + 1]]);
}
return 0;
}
codefoces 22E 图论的更多相关文章
- Codeforces 22E(图论)
题意: 给出n个节点,以及和这个节点指向的节点fi,表示从i能够到达fi,问至少需要添加多少条边能够使得原图变为强连通分量, 输出边数及添加的边,多解输出任意一组解. 2 <= n <= ...
- [leetcode] 题型整理之图论
图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...
- 并查集(图论) LA 3644 X-Plosives
题目传送门 题意:训练指南P191 分析:本题特殊,n个物品,n种元素则会爆炸,可以转移到图论里的n个点,连一条边表示u,v元素放在一起,如果不出现环,一定是n点,n-1条边,所以如果两个元素在同一个 ...
- NOIp 2014 #2 联合权值 Label:图论 !!!未AC
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- HDU 5521 [图论][最短路][建图灵感]
/* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...
- SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...
- [转] POJ图论入门
最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...
- HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Codeforces 553C Love Triangles(图论)
Solution: 比较好的图论的题. 要做这一题,首先要分析love关系和hate关系中,love关系具有传递性.更关键的一点,hate关系是不能成奇环的. 看到没有奇环很自然想到二分图的特性. 那 ...
随机推荐
- Dataphin公共云重磅发布,提供一站式智能数据构建与管理能
点击订阅新品发布会! 新产品.新版本.新技术.新功能.价格调整,评论在下方,下期更新!关注更多内容,了解更多 最新发布 Dataphin公共云重磅发布 2019年6月26日15时,阿里云Dataphi ...
- 模板—扩展GCD*2
有必要重新学一下扩展GCD emmmm. 主要是扩展GCD求解线性同余方程$ax≡b (mod p)$. 1.方程有解的充分必要条件:b%gcd(a,p)=0. 证明: $ax-py=b$ 由于求解整 ...
- 网上很多laravel中cookie的使用方法。
https://blog.csdn.net/chen529834149/article/details/75244718 概述 Cookie的添加其实很简单,直接使用Cookie::make(),在使 ...
- hdu 1596 find the safest road (变形SP && dij+heap)
Problem - 1596 变形最短路问题,给出邻接矩阵,要求求出给定点对间安全率最大值. 这题可以用dijkstra+heap来做.对于每一个查询,做一次dij即可. 代码如下: #include ...
- Google Colab——用谷歌免费GPU跑你的深度学习代码
Google Colab简介 Google Colaboratory是谷歌开放的一款研究工具,主要用于机器学习的开发和研究.这款工具现在可以免费使用,但是不是永久免费暂时还不确定.Google Col ...
- hdu 1255 覆盖的面积 (Bruceforce)
Problem - 1255 暴力统计覆盖超过一次的区域.1y. 代码如下: #include <cstdio> #include <cstring> #include < ...
- hdu 4430 Yukari's Birthday (简单数学 + 二分)
Problem - 4430 题意是,给出蜡烛的数量,要求求出r和k,r是蜡烛的层数,k是每一层蜡烛数目的底数. 开始的时候,没有看清题目,其实中间的那根蜡烛是可放可不放的.假设放置中间的那根蜡烛,就 ...
- Activiti 工作流入门指南
概览 如我们的介绍部分所述,Activiti目前分为两大类: Activiti Core Activiti Cloud 如果你想上手Activiti的核心是否遵循了新的运行时API的入门指南:Acti ...
- JS拉平数组
JS拉平数组 有时候会遇到一个数组里边有多个数组的情况(多维数组),然后你想把它拉平使其成为一个一维数组,其实最简单的一个方法就是ES6数组方法Array.prototype.flat.使用方法如下: ...
- HDU 1754线段树基本操作,建树,更新,查询
代码线段树入门整理中有介绍. #include<cstdio> #include<algorithm> #include<cstring> #include< ...