77562024-01-11 08:02:36TuruTamasHálózatjavításcpp17Wrong answer 0/509ms6812 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
ifstream in_file("minta/be2.txt");
#define input in_file
#define INTHENAMEOFGOD
#else
#define input cin
#define INTHENAMEOFGOD \
    ios::sync_with_stdio(0); \
    cin.tie(0); \
    cout.tie(0);
#endif
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<ll, ll> pii;

ll N, M, a, b, max_korszam, becsat;
vvi G;
vector<pii> kor;
vector<char> vis;

char dfs1(ll x) {
    vis[x] = 1;
    for (ll next : G[x]) {
        if (vis[next] == 1) {
            becsat = next;
            kor[x] = {next, 0};
            return 1;
        }
        if (vis[next] == 2)
            continue;

        char res = dfs1(next);
        if (res == 1) {
            kor[x] = {next, 0};
            if (x == becsat) {
                return 0;
            }
            return 1;
        }
        if (res == 0) {
            return 0;
        }
    }
    vis[x] = 2;
    return 2;
}

void korbejar(ll tol, ll ig) {
    ll most = tol;
    while (most != ig) {
        kor[most].second++;
        most = kor[most].first;
    }
}

void dfs2(ll x, ll kezdx) {
    vis[x] = 1;
    for (ll next : G[x]) {
        if (kor[next].first != -1) {
            korbejar(next, kezdx);
        }
        else if (!vis[next]) {
            dfs2(next, kezdx);
        }
    }
}

int main() {
    INTHENAMEOFGOD
    input >> N >> M;
    G.resize(N);
    for (ll m = 0; m < M; m++) {
        input >> a >> b;
        a--; b--;
        G[a].push_back(b);
    }

    vis.assign(N, 0);
    kor.assign(N, {-1, 0});
    ll x = 0;
    while (x < N && dfs1(x) == 2) {
        x++;
    }
    if (x == N) {
        cout << 0 << endl;
        exit(0);
    }    
    
    vis.assign(N, 0);
    for (ll n = 0; n < N; n++) {
        if (kor[n].first != -1) {
            for (ll next : G[n]) {
                if (!vis[next] && kor[next].first == -1) {
                    dfs2(next, n);
                }
            }
        }
    }

    ll maxfok = max_element(kor.begin(), kor.end(), [](pii a, pii b) { return a.second < b.second; })->second;

    ll db = 0;
    string s;
    ostringstream os(s);

    for (ll n = 0; n < N; n++) {
        if (kor[n].first != -1 && kor[n].second == maxfok) {
            db++;
            os << n+1 << " " << kor[n].first+1 << "\n";
        }
    }
    cout << db << endl;
    // cout << db << "\n" << os.str() << endl;
}
SubtaskSumTestVerdictTimeMemory
base0/50
1Wrong answer0/03ms1976 KiB
2Wrong answer0/09ms4824 KiB
3Wrong answer0/13ms2120 KiB
4Wrong answer0/13ms2336 KiB
5Wrong answer0/13ms2552 KiB
6Wrong answer0/13ms2896 KiB
7Wrong answer0/13ms3112 KiB
8Wrong answer0/24ms3804 KiB
9Wrong answer0/24ms3792 KiB
10Wrong answer0/23ms3416 KiB
11Wrong answer0/24ms4112 KiB
12Wrong answer0/23ms3532 KiB
13Wrong answer0/26ms4588 KiB
14Wrong answer0/26ms4376 KiB
15Wrong answer0/27ms5164 KiB
16Wrong answer0/27ms5128 KiB
17Wrong answer0/28ms5888 KiB
18Wrong answer0/29ms6452 KiB
19Wrong answer0/28ms6284 KiB
20Wrong answer0/28ms6528 KiB
21Wrong answer0/29ms6604 KiB
22Wrong answer0/28ms6548 KiB
23Wrong answer0/39ms6624 KiB
24Wrong answer0/49ms6616 KiB
25Wrong answer0/48ms6576 KiB
26Wrong answer0/49ms6812 KiB