Cod sursa(job #959608)

Utilizator darrenRares Buhai darren Data 8 iunie 2013 14:06:31
Problema Nowhere-zero Scor 10
Compilator cpp Status done
Runda Infoarena Cup 2013 Marime 1.64 kb
#include <fstream>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

int N, M;
int E1[300002], E2[300002];
map<pair<int, int>, int> F;
vector<int> V[100002];
int ST[100002], when[100002];
bool found = false;

int root;
void Dfs(int x)
{
    when[x] = root;
    ST[++ST[0]] = x;

    if (x == root)
    {
        while (ST[0] >= 2)
        {
            ++F[make_pair(ST[ST[0] - 1], ST[ST[0]])];
            --ST[0];
        }
        found = true;
        return;
    }

    for (vector<int>::iterator it = V[x].begin(); it != V[x].end(); ++it)
    {
        if (when[*it] != root && F[make_pair(*it, x)] == 0 && F[make_pair(x, *it)] < 5)
            Dfs(*it);
        if (found) return;
    }

    --ST[0];
}

int main()
{
    ifstream fin("nowhere-zero.in");
    ofstream fout("nowhere-zero.out");

    fin >> N >> M;

    double auxx, auxy;
    for (int i = 1; i <= N; ++i)
        fin >> auxx >> auxy;

    for (int i = 1; i <= M; ++i)
    {
        fin >> E1[i] >> E2[i];
        V[E1[i]].push_back(E2[i]);
        V[E2[i]].push_back(E1[i]);
    }

    for (int i = 1; i <= M; ++i)
        if (F[make_pair(E1[i], E2[i])] == 0 && F[make_pair(E2[i], E1[i])] == 0)
        {
            ++F[make_pair(E1[i], E2[i])];

            root = E1[i];
            ST[0] = 0;
            found = false;

            Dfs(E2[i]);
        }

    for (map<pair<int, int>, int>::iterator it = F.begin(); it != F.end(); ++it)
        if (it->second != 0)
            fout << it->first.first << ' ' << it->first.second << ' ' << it->second << '\n';

    fin.close();
    fout.close();
}