luogu 1726 上白泽惠音
题目大意:
给一个有向图
求一个最大的强连通分量,输出这个强连通分量里的所有元素
若两个联通分量内点数相同 则输出字典序小的那个
思路:
直接tarjan
对每个连通分量,求一下最小点,然后判断字典序就行了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#define ll long long
#define inf 2147383611
#define MAXN 100100
#define MOD 5050
using namespace std;
inline int read()
{
int x=,f=;
char ch;ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-;ch=getchar();}
while(isdigit(ch)) {x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,next[MAXN*],first[MAXN],to[MAXN*],cnt;
int scc,dfn[MAXN],low[MAXN],bl[MAXN],stp,num[MAXN],minn[MAXN];
bool vis[MAXN];
int st[MAXN],top;
void add(int u,int v) {next[++cnt]=first[u],first[u]=cnt,to[cnt]=v;}
void tarjan(int x)
{
low[x]=dfn[x]=++stp,st[++top]=x,vis[x]=;
for(int i=first[x];i;i=next[i])
{
if(!dfn[to[i]])
{
tarjan(to[i]);low[x]=min(low[x],low[to[i]]);
}
else if(vis[to[i]]) low[x]=min(low[x],dfn[to[i]]);
}
if(low[x]==dfn[x])
{
scc++,minn[scc]=inf;
while(st[top+]!=x) minn[scc]=min(st[top],minn[scc]),bl[st[top]]=scc,num[scc]++,vis[st[top]]=,top--;
}
}
int main()
{
n=read(),m=read();
int t,a,b;
while(m--)
{
a=read(),b=read(),t=read();
if(t==) add(a,b);
else {add(a,b);add(b,a);}
}
for(int i=;i<=n;i++) if(!dfn[i]) tarjan(i);
int tmp=,k;
for(int i=;i<=scc;i++) if(num[i]>=tmp||(num[i]==tmp&&minn[i]<minn[tmp])) tmp=num[i],k=i;
printf("%d\n",tmp);
for(int i=;i<=n;i++) if(bl[i]==k) printf("%d ",i);
}
luogu 1726 上白泽惠音的更多相关文章
- 洛谷1726 上白泽慧音 tarjan模板
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- Luogu P1726 上白泽慧音
这显然是一道求强连通分量(SCC)的题目. 只要你正常,都知道应该写Tarjan. 然后(假装会写Tarjan),其实我当然不会.但是求SCC还有另一个算法.复杂度和Tarjan一样,只不过常数大了点 ...
- 【luogu P1726 上白泽慧音】 题解
题目链接:https://www.luogu.org/problemnew/show/P1726 菜 #include <stack> #include <cstdio> #i ...
- CODEVS——T1332 上白泽慧音 || 洛谷——P1726 上白泽慧音
http://codevs.cn/problem/1332/|| https://www.luogu.org/problem/show?pid=1726#sub 时间限制: 1 s 空间限制: 1 ...
- 洛谷P1726 上白泽慧音 [Tarjan]
题目传送门 上白泽慧音 题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村 ...
- 洛谷P1726 上白泽慧音
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- P1726 上白泽慧音
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- 上白泽慧音(tarjan,图的染色)
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- codevs 1332 上白泽慧音
1332 上白泽慧音 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大 ...
随机推荐
- Linux修改启动界面、分辨率
初识Linux 初识Linux(Centos 7.x),积累一些小技巧. 修改命令行界面的分辨率 # 备份配置文件 # 有些系统路径是/boot/grub...或者/boot/grub/menu.ls ...
- tomcat排错以及优化
jstack $PID #查看java进程的状态,分析tomcat卡死原因,定位java进程卡死的函数,调整代码 #RUNNABLE,在虚拟机内执行的.运行中状态,可能里面还能看到locked字样,表 ...
- 15Spring AOP基础
为什么需要AOP? 先来看一段代码: package com.cn.spring.aop.helloworld; //加减乘除的接口类 public interface ArithmeticCalcu ...
- Python之爬虫-酷6视频
Python之爬虫-酷6视频 #!/usr/bin/env python # -*- coding:utf-8 -*- import re import requests response = req ...
- 杭电 1009 FatMouse' Trade (贪心)
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
- jQuery实现上传进度条效果
效果:(点击上传按钮) See the Pen pjGNJr by moyu (@MoYu1991) on CodePen. html代码: <!DOCTYPE html> <h ...
- CSU 1225 最长上升子序列并记录其个数
;j<i;j++){ if(h[i] > h[j]){ ) cnt[i]+=cnt[j]; ) len[i] = len[j] + , cnt[i] = cnt[j]; } //身高相同的 ...
- hdu 1075
#include<stdio.h> #include<string.h> #include<string> #include<iostream> #in ...
- CF901C. Bipartite Segments
n<=300000,m<=300000的图,图上只有奇环,q<=300000个询问每次问:一个区间内有多少个子区间,满足只保留编号在该区间的点以及他们之间的边,可以构成一个二分图. ...
- vimtips阅读记录
__BEGIN__ *vimtips.txt* For Vim version 8.0. ------------------------------------------------------- ...