P1726 上白泽慧音 tarjan 模板
这是一道用tarjan做的模板,要求找到有向图中最大的联通块。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
/*-----------------------showtime----------------------*/
const int maxn = 1e5+;
struct E{
int u,v;
int nxt;
}edge[maxn];
int head[maxn],gtot = ;
void addedge(int u,int v){
edge[gtot].u = u;
edge[gtot].v = v;
edge[gtot].nxt = head[u];
head[u] = gtot++;
} priority_queue<int,vector<int>,greater<int> >tmp,ans;//这是一个小根堆q int vis[maxn],dfn[maxn],low[maxn];
stack<int>sk;
int tot = ;
void tarjan(int u){
tot++;
dfn[u] = low[u] = tot;
vis[u] = ; sk.push(u); for(int i=head[u]; ~i; i = edge[i].nxt){
int v = edge[i].v;
if(dfn[v] == ) {
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(vis[v]){
low[u] = min(low[u], dfn[v]);
}
} if(dfn[u] == low[u]){ while(!tmp.empty())tmp.pop(); while(!sk.empty() && sk.top() != u){
vis[sk.top()] = ;
tmp.push(sk.top()); sk.pop();
}
vis[sk.top()] = ;
tmp.push(sk.top()); sk.pop(); if(tmp.size() > ans.size()) ans = tmp;
else if(tmp.size() == ans.size() && tmp.top() < ans.top()) ans = tmp;
}
}
int main(){
int n,m;
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
for(int i=; i<=m; i++){
int u,v,op;
scanf("%d%d%d", &u, &v, &op);
if(op == ) addedge(u,v);
else {
addedge(u,v);
addedge(v,u);
}
}
for(int i=; i<=n; i++)
if(dfn[i] == )tarjan(i);
printf("%d\n", (int)ans.size());
while(!ans.empty()){
printf("%d ", ans.top());
ans.pop();
}
puts("");
return ;
}
P1726 上白泽慧音 tarjan 模板的更多相关文章
- 洛谷1726 上白泽慧音 tarjan模板
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- 洛谷P1726 上白泽慧音 [Tarjan]
题目传送门 上白泽慧音 题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村 ...
- 洛谷P1726 上白泽慧音(Tarjan强连通分量)
P1726 上白泽慧音 题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村 ...
- CODEVS——T1332 上白泽慧音 || 洛谷——P1726 上白泽慧音
http://codevs.cn/problem/1332/|| https://www.luogu.org/problem/show?pid=1726#sub 时间限制: 1 s 空间限制: 1 ...
- 洛谷P1726 上白泽慧音
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- P1726 上白泽慧音
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- 洛谷 P1726 上白泽慧音
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- P1726 上白泽慧音(0分)
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- Luogu P1726 上白泽慧音
这显然是一道求强连通分量(SCC)的题目. 只要你正常,都知道应该写Tarjan. 然后(假装会写Tarjan),其实我当然不会.但是求SCC还有另一个算法.复杂度和Tarjan一样,只不过常数大了点 ...
随机推荐
- WPF 动态添加按钮以及样式字典的引用(Style introduction)
我们想要达到的结果是,绑定多个Checkbox然后我们还可以获取它是否被选中,其实很简单,我们只要找到那几个关键的对象就可以了. 下面是Ui,其中定义了一个WrapPanel来存放CheckBox,还 ...
- 在Linux - Centos上安装Python3(上)
必看内容 在Linux上安装Python常用的2种方法 1.Python源码编译安装,有点复杂,适合老司机 2.从EPEL/IUS仓库安装,新手建议使用些方法,比较简单,目前2019-07-31提供最 ...
- 【WPF】 InkCanvas 书写毛笔效果
首先贴出本文参考学习的文章吧. https://www.cnblogs.com/LCHL/p/9055642.html#4206298 感谢这位懒羊羊的代码和讲解(下简称羊博主),我在此基础上稍微加了 ...
- Apache ActiveMQ 实践 <二>
一.订阅/发布模式 1.生产者 /** * 消息生产者 * */public class JMSProducer { private static final String USERNAME=Acti ...
- MyBatis 核心配置综述之StatementHandler
目录 MyBatis 核心配置综述之StatementHandler MyBatis 四大组件之StatementHandler StatementHandler 的基本构成 StatementHan ...
- JDK的命令行工具系列 (一) jps、jstat
概述 在我们进行故障定位和性能分析时, 可以使用Java Dump(也叫Dump文件)来帮助排查问题, 它记录了JVM运行期间的内存占用和线程执行等情况.其中Heap Dump文件是二进制格式, 它保 ...
- Vue组件间通信-Vuex
上回说到Vue组件间通讯,最后留了一个彩蛋~~~Vuex.Vuex是另一种组件通讯的方法,这节来说说Vuex(store仓库). 首先Vuex需要安装,安装的方式有很多,在这里就不一一细说了.我是通过 ...
- 深入理解JVM-类加载器深入解析(1)
类加载 在java代码中,类型的加载,连接与初始化过程都是在程序运行期间完成的 类型:表示的Object本身,并不是指一个对象,也就是class. 运行期间:表示的是一种runtime的概念,在运行期 ...
- 记一次paramiko远程连接遇到的坑
背景:工作中遇到了一个问题,需要用到windows向windows连接(文件传发)以及,linux向windows连接(文件传发)的需求. 自然而然会考虑到用paramiko,然而paramiko我用 ...
- python_Tensorflow学习(三):TensorFlow学习基础
一.矩阵的基本操作 import tensorflow as tf # 1.1矩阵操作 sess = tf.InteractiveSession() x = tf.ones([2, 3], &qu ...