sgu 219 分类: sgu 2015-06-23 13:39 16人阅读 评论(0) 收藏


题面很长,其实是道傻逼题,没什么好说的。。。



#include <cstdio>
#include <cstdlib>
#include <queue>
#include <iostream>
#include <algorithm>

const int maxn = 1005, maxm = 50005;

struct Edge
{
    int v, w, next;
    Edge(){}
    Edge(int v,int w,int next):v(v),w(w),next(next){}
}edge[maxm];
int n, m, head[maxn], el;
int rd[maxn], check[maxn];
std::queue<int> line;

void NewEdge(int u,int v,int w)
{
    edge[++el] = Edge(v, w, head[u]), head[u] = el;
}
void Troop()
{
    for(int i = 1; i <= n; i++)
        if(!rd[i]) line.push(i);

    while(!line.empty())
    {
        int t = line.front();
        check[t] = 1, line.pop();

        for(int i = head[t], p; i; i = edge[i].next)
            if(!edge[i].w && !check[p = edge[i].v])
                if(!(--rd[p])) line.push(p);    
    }
}
void DFS()
{
    for(int i = 1; i <= n; i++)
        if(!check[i]) line.push(i);

    while(!line.empty())
    {
        int t = line.front();
        line.pop();

        for(int i = head[t], p; i; i = edge[i].next)
            if(check[p = edge[i].v])
                check[p] = 0, line.push(p);     
    }   
}


int main()
{
#ifndef ONLINE_JUDGE
    freopen("sgu219.in","r",stdin);
    freopen("sgu219.out","w",stdout);
#endif

    std::cin >> n >> m;
    for(int i = 1, u, v, w; i <= m; i++)
        scanf("%d%d%d",&u,&v,&w), NewEdge(u, v, w);

    for(int i = 1; i <= el; i++)
        if(!edge[i].w) rd[edge[i].v] ++;

    Troop(), DFS();

    for(int i = 1; i <= n; i++)
        printf("%d\n",check[i]);

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;           
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-06-23 13:39  <Dash>  阅读(174)  评论(0编辑  收藏  举报