Cod sursa(job #3310088)

Utilizator AndreiNicolaescuEric Paturan AndreiNicolaescu Data 11 septembrie 2025 17:49:32
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <bits/stdc++.h>
#define cin ci
#define cout co
using namespace std;
ifstream cin("party.in");
ofstream cout("party.out");
int n, m, x, y, notx, noty, c, cmp;
vector<vector<int>> graf, graft;
vector<int> viz, ord, comp;
void dfs(int node)
{
    viz[node] = 1;
    for(auto next:graf[node])
        if(!viz[next])
            dfs(next);
    ord.push_back(node);
}
void dfst(int node)
{
    viz[node] = 1;
    comp[node] = cmp;
    for(auto next:graft[node])
        if(!viz[next])
            dfst(next);
}
int main()
{
    cin >> n >> m;
    graf.assign(2 * n + 1, vector<int>());
    graft.assign(2 * n + 1, vector<int>());
    viz.resize(2 * n + 1);
    comp.resize(2 * n + 1);
    while(m--)
    {
        cin >> x >> y >> c;
        notx = n + x;
        noty = n + y;
        if(c == 0)
        {
            graf[notx].push_back(y);
            graf[noty].push_back(x);
            graft[y].push_back(notx);
            graft[x].push_back(noty);
        }
        if(c == 1)
        {
            graf[notx].push_back(noty);
            graf[y].push_back(x);
            graft[x].push_back(y);
            graft[noty].push_back(notx);
        }
        if(c == 2)
        {
            graf[noty].push_back(notx);
            graf[x].push_back(y);
            graft[notx].push_back(noty);
            graft[y].push_back(x);
        }
        if(c == 3)
        {
            graf[x].push_back(noty);
            graf[y].push_back(notx);
            graft[noty].push_back(x);
            graft[notx].push_back(y);
        }
    }
    for(int i=1; i<= 2 * n; i++)
        if(!viz[i])
            dfs(i);
    fill(viz.begin(), viz.end(), 0);
    reverse(ord.begin(), ord.end());
    for(auto i:ord)
        if(!viz[i])
        {
            cmp++;
            dfst(i);
        }
    int nr = 0;
    vector<int> ans;
    for(int i=1; i<=n; i++)
        if(comp[i] > comp[n + i])
        {
            nr++;
            ans.push_back(i);
        }
    cout << nr << '\n';
    for(auto i : ans)
        cout << i << '\n';
    return 0;
}