Cod sursa(job #1171640)

Utilizator RazvanR104Razvan-Andrei Ciocoiu RazvanR104 Data 15 aprilie 2014 23:51:34
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.94 kb
#include <fstream>

#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#if __cplusplus > 199711L
#include <unordered_map>
#include <unordered_set>
#endif

#include <cmath>
#include <cstring>
#include <cstdlib>

using namespace std;

const int inf = 0x3f3f3f3f;

int T, N, M;
int cost[15010], deg[15010], maxc[15010];
vector<int> Graph[15010], _Graph[15010], Q;

int main()
{
	ifstream fin("easygraph.in");
	ofstream fout("easygraph.out");

	int i, x, y, node, sz, res, j;
	vector<int>::iterator it, _it;

	for (fin >> T; T; --T)
    {
        fin >> N >> M;

        for (i = 1; i <= N; ++i)
        {
            fin >> cost[i];
            Graph[i].clear();
            _Graph[i].clear();
        }

        while(M--)
        {
            fin >> x >> y;
            Graph[x].push_back(y);
            _Graph[y].push_back(x);
            ++deg[y];
        }

        for (i = 1; i <= N; ++i)
            if (!deg[i])
            {
                Q.push_back(i);
                maxc[i] = cost[i];
            }

        sz = Q.size();

        for (i = 0; i < N; ++i)
        {
            node = Q[i];

            for (it = Graph[node].begin(); it != Graph[node].end(); ++it)
            {
                --deg[*it];
                if (!deg[*it])
                    Q.push_back(*it);
            }
        }

        for (i = sz; i < N; ++i)
        {
            node = Q[i];

            for (it = _Graph[node].begin(); it != _Graph[node].end(); ++it)
                maxc[node] = max(cost[node], cost[node] + maxc[*it]);
        }

        res = -inf;
        for (i = 1; i <= N; ++i)
            res = max(res, maxc[i]);

        fout << res << ' ';

        memset(deg, 0, sizeof(deg));
        Q.clear();
    }

    fout << '\n';

	fout.close();
    return 0;
}