【luogu P1726 上白泽慧音】 题解
2024-09-17 11:33:11
题目链接:https://www.luogu.org/problemnew/show/P1726
菜
#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 50000 + 10;
int n, m, root;
struct edge{
int from, to, next;
}e[maxn<<2];
int head[maxn], cnt;
int dfn[maxn], low[maxn], color[maxn], tim, num, tong[maxn];
bool vis[maxn];
stack<int> s;
void add(int u, int v)
{
e[++cnt].from = u;
e[cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
}
void tarjan(int x)
{
dfn[x] = low[x] = ++tim;
s.push(x); vis[x] = 1;
for(int i = head[x]; i != -1; i = e[i].next)
{
int v = e[i].to;
if(!dfn[v])
{
tarjan(v);
low[x] = min(low[x], low[v]);
}
else if(vis[v])
{
low[x] = min(low[x], low[v]);
}
}
if(dfn[x] == low[x])
{
color[x] = ++num;
vis[x] = 0;
while(s.top() != x)
{
color[s.top()] = num;
vis[s.top()] = 0;
s.pop();
}
s.pop();
}
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d",&n,&m);
for(int i = 1; i <= m; i++)
{
int u, v, w;
scanf("%d%d%d",&v,&u,&w);
if(w == 1)
{
add(u, v);
}
else
{
add(u, v), add(v, u);
}
}
for(int i = 1; i <= n; i++)
if(!dfn[i]) tarjan(i);
for(int i = 1; i <= n; i++)
tong[color[i]]++;
int ans = -1, flag;
for(int i = 1; i <= num; i++)
{
if(ans < tong[i])
{
ans = tong[i];
flag = i;
}
}
printf("%d\n",ans);
for(int i = 1; i <= n; i++)
if(color[i] == flag) printf("%d ",i);
return 0;
}
最新文章
- C#获取屏幕鼠标所指点的颜色
- Union-Find 检测无向图有无环路算法
- Javascript本质第一篇:核心概念
- 移动前端开发-单页应用(spa)模型
- Visual Studio 2013支持Xamarin的解决方案
- test 2017-1-5
- ffmpeg 音频转换: use ffmpeg convert the audio from stereo to mono without changing the video part
- 初学C++之自定义类型名简化
- [最近公共祖先] POJ 3728 The merchant
- C++概念整理
- 推荐一款C#反编译软件(开源)
- 同一个tomcat多个web应用共享session
- css pre如果同时运用了css的border-radius、 overflow两个属性且标签中内容太多时,外部div滚动条在firefox下滚动时很卡
- ASP.NET动态引用WebService接口
- 转自scutan 常用的Linux编程库
- 在MVC中使用rdlc格式的报表
- 项目Beta冲刺Day2
- ABB机器人基础培训资料整理与总结
- js判断对象是否为空
- 01day