P1726 上白泽慧音

这是一道用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 ;
}

最新文章

  1. ThinkPHP 3.2.3 URL 路由的使用
  2. poj 1383 Labyrinth
  3. Java调用外部程序常用算法和封装类
  4. android 颜色收集及部分gridview样式设置
  5. Docker命令使用详解
  6. 图解JS的传值与传址
  7. [转]Android Shape渲染的使用(经典,学习研究不后悔)
  8. 浅谈web应用的负载均衡、集群、高可用(HA)解决方案(转)
  9. bootstrap中下拉菜单点击事件 uncaught syntaxerror unexpected end of input异常问题
  10. [Mysql] &quot;Too many connections&quot;
  11. AES加密解密——AES在JavaWeb项目中前台JS加密,后台Java解密的使用
  12. visual filters 滤镜 ie
  13. python项目依赖管理分享迁移后重建开发环境(一)virtualenv 和 pip
  14. Git .gitignore文件说明
  15. Stateful Future Transformation
  16. 限制ssh登录ip和系统用户
  17. C++11并发编程实战 免费书籍
  18. java 多线程 23 : Timer
  19. switch_to_frame,切换frame框架
  20. 【辅助工具】Python实现微信跳一跳

热门文章

  1. RabbitMQ搭建单机及集群
  2. java中map,set的简单使用
  3. Echarts图表插件(4.x版本)使用(二、带分类筛选的多个图表/实例化多个ECharts,以关系图/force为例)
  4. HelloDjango 系列教程:博客从“裸奔”到“有皮肤”
  5. springMVC(一) --前端控制器(DispatcherServlet)的作用
  6. 11个rsync使用实例
  7. 通过注解实现通用导出Excel
  8. 爬虫之爬取电影天堂(request)
  9. yaml文件解析详解
  10. 当我们尝试用JavaScipt测网速