上白泽慧 洛谷1726 强连通分量

题目描述


在幻想乡,上白泽慧音是以知识渊博闻名的老师。春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄。因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点。人间之里由N个村庄(编号为1..N)和M条道路组成,道路分为两种一种为单向通行的,一种为双向通行的,分别用1和2来标记。如果存在由村庄A到达村庄B的通路,那么我们认为可以从村庄A到达村庄B,记为(A,B)。当(A,B)和(B,A)同时满足时,我们认为A,B是绝对连通的,记为

输入输出格式


输入格式:


第1行:两个正整数N,M
第2..M+1行:每行三个正整数a,b,t, t = 1表示存在从村庄a到b的单向道路,t = 2表示村庄a,b之间存在双向通行的道路。保证每条道路只出现一次。

输出格式:


第1行: 1个整数,表示最大的绝对连通区域包含的村庄个数。
第2行:若干个整数,依次输出最大的绝对连通区域所包含的村庄编号。

输入输出样例


输入样例#1:


5 5
1 2 1
1 3 2
2 4 2
5 1 2
3 5 1

输出样例#1:


3
1 3 5

说明


对于60%的数据:N <= 200且M <= 10,000
对于100%的数据:N <= 5,000且M <= 50,000

题解


怎么说呢,看到双向边的我否定了强连通分量,然并卵我还是转tarjan接着1A了
这是第一道爬虫抓下来的洛谷题目介绍,感觉反而更麻烦了。。

Code


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define debug puts("-----")
#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)
#define drp(i, st, ed) for (int i = st; i >= ed; i -= 1)
#define fill(x, t) memset(x, t, sizeof(x))
#define min(x, y) x<y?x:y
#define max(x, y) x>y?x:y
#define PI (acos(-1.0))
#define EPS (1e-8)
#define INF (1<<30)
#define ll long long
#define db double
#define ld long double
#define N 5001
#define E N * 16 + 1
#define MOD 100000007
#define L 255
using namespace std;
struct edge{
    int x, y, w, next;
}e[E];
int inStack[N], size[N], scc[N], dfn[N], low[N], ls[N];
int cnt;
stack<int>s;
inline int read(){
    int x = 0, v = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9'){
        if (ch == '-'){
            v = -1;
        }
        ch = getchar();
    }
    while (ch <= '9' && ch >= '0'){
        x = (x << 1) + (x << 3) + ch - '0';
        ch = getchar();
    }
    return x * v;
}
inline int addEdge(int &cnt, const int &x, const int &y, const int &w = 1){
    e[++ cnt] = (edge){x, y, w, ls[x]}; ls[x] = cnt;
    return 0;
}
inline int dfs(int now){
    dfn[now] = low[now] = ++ cnt;
    inStack[now] = 1;
    s.push(now);
    for (int i = ls[now]; i; i = e[i].next){
        if (!dfn[e[i].y]){
            dfs(e[i].y);
            low[now] = min(low[now], low[e[i].y]);
        }else if (inStack[e[i].y]){
            low[now] = min(low[now], dfn[e[i].y]);
        }
    }
    if (low[now] == dfn[now]){
        scc[0] += 1;
        for (int tmp = 0; tmp != now; ){
            tmp = s.top(); s.pop();
            scc[tmp] = scc[0];
            inStack[tmp] = 0;
            size[scc[0]] += 1;
        }
    }
}
inline int tarjan(const int &n){
    fill(inStack, 0);
    fill(size, 0);
    fill(dfn, 0);
    fill(low, 0);
    cnt = 0;
    rep(i, 1, n){
        if (!dfn[i]){
            dfs(i);
        }
    }
}
int main(void){
    int n = read(), m = read();
    int edgeCnt = 1;
    rep(i, 1, m){
        int x = read(), y = read(), opt = read() - 1;
        if (opt){
            addEdge(edgeCnt, x, y);
            addEdge(edgeCnt, y, x);
        }else{
            addEdge(edgeCnt, x, y);
        }
    }
    tarjan(n);
    int ans = 0;
    rep(i, 1, n){
        ans = max(ans, size[scc[i]]);
    }
    printf("%d\n", ans);
    rep(i, 1, n){
        if (ans == size[scc[i]]){
            printf("%d ", i);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值