上白泽慧音

Luogu P1726 上白泽慧音

题目描述

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

人间之里由 \(N\) 个村庄(编号为 \(1\cdots N\))和 \(M\) 条道路组成,道路分为两种,一种为单向通行的,一种为双向通行的,分别用 \(1\) 和 \(2\) 来标记。如果存在由村庄 \(A\) 到达村庄 \(B\) 的通路,那么我们认为可以从村庄 \(A\) 到达村庄 \(B\),记为 \((A,B)\)。当 \((A,B)\) 和 \((B,A)\) 同时满足时,我们认为 \(A,B\) 是绝对连通的,记为 \(\langle A,B\rangle\)。绝对连通区域是指一个村庄的集合,在这个集合中任意两个村庄 \(X,Y\) 都满足 \(\langle X,Y\rangle\)。现在你的任务是,找出最大的绝对连通区域,并将这个绝对连通区域的村庄按编号依次输出。若存在两个最大的,输出字典序最小的,比如当存在 \(1,3,4\) 和 \(2,5,6\) 这两个最大连通区域时,输出的是 \(1,3,4\)。

输入格式

第一行共两个正整数 \(N,M\)。

第 \(2\) 行至第 \(M+1\) 行,每行有三个正整数 \(a,b,t\)。若 \(t = 1\) 则表示存在从村庄 \(a\) 到 \(b\) 的单向道路,若 \(t = 2\) 表示村庄 \(a,b\) 之间存在双向通行的道路。保证每条道路只出现一次。

输出格式

第一行输出 \(1\) 个整数,表示最大的绝对连通区域包含的村庄个数。

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

输入输出样例 #1

输入 #1

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

输出 #1

3
1 3 5

说明/提示

  • 对于 \(60\%\) 的数据,\(1\le N \le 200\),且 \(0\le M \le 10^4\);
  • 对于 \(100\%\) 的数据,\(1\le N \le 5\times 10^3\),且 \(0\le M \le 5\times 10^4\)。

Code

考前最后一题,妙趣横生的SCC题,难点主要是处理输出

#include <bits/stdc++.h>
#define maxn 20001
#define maxm 200001

using namespace std;

int n, m, cnt = 1, head[maxn], to[maxm], nxt[maxm];
int t, dfn[maxn], low[maxn];
stack<int> s;
bool ins[maxn];
vector<int> scc[maxn];
int tot;

void add(int u, int v)
{
    to[++cnt] = v;
    nxt[cnt] = head[u];
    head[u] = cnt;
}

void tarjan(int u)
{
    dfn[u] = low[u] = ++t;
    s.push(u);
    ins[u] = 1;
    for (int i = head[u]; i; i = nxt[i])
    {
        int v = to[i];
        if (!dfn[v])
        {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if (ins[v])
        {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u])
    {
        tot++;
        while (!s.empty())
        {
            int x = s.top();
            s.pop();
            ins[x] = 0;
            scc[tot].push_back(x);
            if (x == u)
            {
                break;
            }
        }
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        if (c == 2)
        {
            add(a, b);
            add(b, a);
        }
        else
        {
            add(a, b);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (!dfn[i])
        {
            tarjan(i);
        }
    }
    int res = 0;
    string s = "";
    for (int i = 1; i <= tot; i++)
    {
        sort(scc[i].begin(), scc[i].end());
        int sz = scc[i].size();
        if (sz > res)
        {
            res = sz;
            s = "";
            for (auto x : scc[i])
            {
                s += to_string(x);
                s += ' ';
            }
        }
        else if (sz == res)
        {
            string tmp = "";
            for (auto x : scc[i])
            {
                tmp += to_string(x);
                tmp += ' ';
            }
            if (tmp < s)
            {
                s = tmp;
            }
        }
    }
    cout << res << '\n';
    cout << s;
    return 0;
}
posted @ 2026-10-30 21:33  Accepted01  阅读(11)  评论(0)    收藏  举报