Cod sursa(job #3163619)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 31 octombrie 2023 18:17:09
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;

ifstream fin("party.in");
ofstream fout("party.out");

const int maxN = 100005;

int n, m, comp[2 * maxN], nrctc;
vector<int> Gback[2 * maxN];
vector<int> G[2 * maxN];
vector<int> ordine;
bool used[2 * maxN];

int noot(int x) {
    if(x > n)
        return x - n;
    else return x + n;
}

void dfs1(int nod) {
    used[nod] = 1;
    for(int vecin : G[nod])
        if(!used[vecin])
            dfs1(vecin);
    
    ordine.push_back(nod);
}

void dfs2(int nod) {
    used[nod] = 1;
    comp[nod] = nrctc;
    for(int vecin : Gback[nod])
        if(!used[vecin])
            dfs2(vecin);
}

void change (int &x, int &y, int c) {
    if(c == 0)
        return;

    if(c == 1)
        y = y + n;
    
    if(c == 2)
        x = x + n;
    
    if(c == 3) {
        x = x + n;
        y = y + n;
    }
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int x, y, c;
        fin >> x >> y >> c;
        change(x, y, c);
        G[noot(x)].push_back(y);
        G[noot(y)].push_back(x);
        Gback[x].push_back(noot(y));
        Gback[y].push_back(noot(x));
    }
    
    for(int i = 1; i <= 2 * n; i++)
        if(!used[i])
            dfs1(i);
    
    for(int i = 1; i <= 2 * n; i++)
        used[i] = 0;
    
    reverse(ordine.begin(), ordine.end());
    for(int elem : ordine) {
        if(used[elem])
            continue;
        nrctc++;
        dfs2(elem);
    }

    vector <int> ans;
    for(int i = 1; i <= n; i++)
        if(comp[i] > comp[n + i])
            ans.push_back(i);
    
    fout << ans.size() << '\n';
    for(int elem : ans)
        fout << elem << '\n';
    
    return 0;
}