题目链接: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;
}

【luogu P1726 上白泽慧音】 题解的更多相关文章

  1. Luogu P1726 上白泽慧音

    这显然是一道求强连通分量(SCC)的题目. 只要你正常,都知道应该写Tarjan. 然后(假装会写Tarjan),其实我当然不会.但是求SCC还有另一个算法.复杂度和Tarjan一样,只不过常数大了点 ...

  2. 洛谷P1726 上白泽慧音(Tarjan强连通分量)

    P1726 上白泽慧音 题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村 ...

  3. P1726 上白泽慧音 tarjan 模板

    P1726 上白泽慧音 这是一道用tarjan做的模板,要求找到有向图中最大的联通块. #include <algorithm> #include <iterator> #in ...

  4. 洛谷P1726 上白泽慧音 [Tarjan]

    题目传送门 上白泽慧音 题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村 ...

  5. CODEVS——T1332 上白泽慧音 || 洛谷——P1726 上白泽慧音

    http://codevs.cn/problem/1332/|| https://www.luogu.org/problem/show?pid=1726#sub  时间限制: 1 s  空间限制: 1 ...

  6. 洛谷P1726 上白泽慧音

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

  7. P1726 上白泽慧音

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

  8. 洛谷 P1726 上白泽慧音

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

  9. P1726 上白泽慧音(0分)

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

随机推荐

  1. ckeditor和ckfinder

    ckeditor是一个所见即所得的富文本编辑器,用来代替drupal自带的编辑器. 但是从drupal.com下载的ckeditor模块本身没有实现功能,它指向了由cdn.ckeditor.com所提 ...

  2. 记录: Win10+Ubuntu18.04双系统安装

    在重装windows系统的时候顺便将ubuntu也重装了. window 10 安装 制作USB启动盘 到"微软中国下载中心"(http://www.microsoft.com/z ...

  3. NSTimer_Block封装定时器的target-action成Block回调

    前言 定时器NSTimer虽然简单易用,但是目标响应机制(target-action)这种方式很容易在代码中出现代码臃肿的情况,特别是在一个文件中有大量的代码,多个定时器的时候不方便调试,因此将NST ...

  4. HashTable 元素的查找

    Hashtable 特点:键与值成对存在,键是唯一的,不能重复.在查找元素的时候,我们往往是依据键区查找值的 三种方法 contains   包含 containsKey containsValue ...

  5. 在 Azure Web 应用中创建 Java 应用程序

    本分步指南将通过 Azure Web 应用帮助您启动并运行示例 Java 应用程序.除 Java 外,Azure Web 应用还支持其他语言,如 PHP..NET.Node.JS.Python.Rub ...

  6. Fragment中的方法findFragmentById(int id)的返回值探讨

    在学习<Android编程权威指南>P124页的时候,遇到了这样的代码: 引起了我的疑问if的判断条件是(fragment==null),那执行完上一句 Fragment Fragment ...

  7. Python 动态加载 Extension Manager Classes

    看着看着发现了一个库:stevedore(http://stevedore.readthedocs.org/en/latest/managers.html),但是感觉文档做得不行啊,都没个tutori ...

  8. WSAAsyncSelect 消息模型

    select 模型虽然可以管理多个socket,但是它涉及到一个时机的问题,select模型会针对所管理的数组中的每一个socket循环检测它管理是否在对应的数组中,从时间复杂度上来说它是O(n^2) ...

  9. SharePoint 2013 - Add-ins

    1. App Web & Host Web The special website to which the app is deployed is called an App Web. The ...

  10. PHP 运用rsa加密和解密实例

    1.加密解密的第一步是生成公钥.私钥对,私钥加密的内容能通过公钥解密(反过来亦可以) 下载开源RSA密钥生成工具openssl(通常Linux系统都自带该程序),解压缩至独立的文件夹,进入其中的bin ...