151322025-02-13 11:32:14tamasmarkA lehető legkevesebb metróval utazás (40 pont)cpp17Futási hiba 0/401ms564 KiB
// erosen osszefuggove alakitas.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <set>

using namespace std;

int n, m, a, b, i, j,db,k,v;
struct adat
{
    int lat,rgraf,cspbe,cspki,lat2 ;
    vector<int>sz;
};
struct adat2
{
    int lat, csp;
    vector<int>sz2;
};
vector<adat>x;
vector<int>elek;
void melysegi(int csp)
{
    for (auto& e : x[csp].sz)
    {
        if (!x[e].lat)
        {
            x[e].lat = x[csp].lat;
            melysegi(e);
            if (x[e].rgraf)
            {
                x[csp].rgraf=x[e].lat;
            }
            else
            {
                x[e].lat = 0;
                elek.push_back(csp);
            }
        }
        else if (x[e].lat == x[csp].lat&&x[e].rgraf)
        {
            x[csp].rgraf = x[csp].lat;

        }
        else if (x[e].lat == x[csp].lat)
        {
            x[e].lat = 0;
            x[csp].lat = 0;
        }
        else if(x[e].lat!=x[csp].lat)
        {
            elek.push_back(csp);
        }
        
    }
}
void melysegi2(int csp)
{
    for (auto& e : x[csp].sz)
    {
        if (x[e].lat!=x[csp].lat)
        {
            x[x[csp].lat].cspki++;
            x[x[e].lat].cspbe++;

        }

    }
}
int main()
{
    cin >> n >> m;
    x.resize(n + 1);
    for (i = 1; i <= m; ++i)
    {
        cin >> a >> b;
        x[a].sz.push_back(b);
    }
    db = -1;

    for (i = 1; i <= n; ++i)
    {
        if (!x[i].lat)
        {
            db++;
            x[i].lat = i;
            x[i].rgraf = i;
            melysegi(i);
        }
    }

    for (auto& e : elek)
    {
        melysegi2(e);
    }
    for (i = 1; i <= n; ++i)
    {
        if (i == x[i].lat)
        {
            if (x[i].cspbe == 0) v = i;
            if (x[i].cspki == 0) k = i;
        }
    }
    if (db != 0) cout << k << " " << v;
    else cout << 0 << " " << 0;
    return 0;
}
/*
10 15
2 3
3 6
6 2
3 4
4 2
4 1
1 5
5 10
10 1
1 8
5 7
7 9
9 7
3 7
8 10

3 3
1 2
2 3
3 1

7 8
1 2
2 3
3 1
3 4
4 5
5 6
6 7
7 5

5 6
1 2
3 1
3 2
3 5
5 4
4 3
*/
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
RészfeladatÖsszpontTesztVerdiktIdőMemória
base0/40
1Futási hiba0/01ms316 KiB
2Futási hiba0/01ms316 KiB
3Futási hiba0/21ms316 KiB
4Futási hiba0/21ms316 KiB
5Futási hiba0/21ms316 KiB
6Futási hiba0/21ms316 KiB
7Futási hiba0/21ms316 KiB
8Futási hiba0/21ms316 KiB
9Futási hiba0/21ms508 KiB
10Futási hiba0/21ms564 KiB
11Futási hiba0/21ms316 KiB
12Futási hiba0/21ms508 KiB
13Futási hiba0/21ms316 KiB
14Futási hiba0/21ms316 KiB
15Futási hiba0/21ms316 KiB
16Futási hiba0/21ms316 KiB
17Futási hiba0/21ms316 KiB
18Futási hiba0/21ms316 KiB
19Futási hiba0/21ms316 KiB
20Futási hiba0/21ms392 KiB
21Futási hiba0/21ms512 KiB
22Futási hiba0/21ms348 KiB