拓扑排序hihocoder#1174&&#1175

#1174

#include<string.h>   
#include<queue>
#include<iostream>
#include<stdio.h>
#include<fstream>
using namespace std;   
#define FOR(i,b,e) for(int i=(b);i<=(e);i++)  
#define FORE(i,b,e) for(int i=(b);i>=(e);i--)  
#define maxnum  100003

int inside[maxnum];
struct p{
    p* next;
    int val;
    p* newest;
}*rel[maxnum];




int main()    
{  
    int T,N,M,A,B;
#ifdef DEBUG_
    ifstream fin("G:/1.txt");
#define cin fin
#endif
    //cin>>T;
    scanf("%d",&T);
    FOR(i,1,T){
        memset(inside,0,sizeof(inside));
        memset(rel,0,sizeof(rel));
        queue<int> q;

        //cin>>N>>M;
        scanf("%d%d",&N,&M);
        FOR(j,1,M){
            //cin>>A>>B;
            scanf("%d%d",&A,&B);
            inside[B]+=1;
            if(rel[A]==NULL){
                rel[A]=new p;
                rel[A]->newest=rel[A];
            }else{
                rel[A]->newest->next=new p;
                rel[A]->newest=rel[A]->newest->next;
            }
            rel[A]->newest->val=B;
            rel[A]->newest->next=NULL;
        }
        FOR(i,1,N){
            if(inside[i]==0)
                q.push(i);
        }

        int num=N;
        p* pi;
        while(!q.empty()){
            num-=1;
            pi = rel[q.front()];
            q.pop();
            while(pi!=NULL){
                inside[pi->val]-=1;
                if(!inside[pi->val])
                    q.push(pi->val);
                pi=pi->next;
            }
        }
        if(num==0)
            //cout<<"Correct"<<endl;
            printf("Correct\n");
        else
            //cout<<"Wrong"<<endl;
            printf("Wrong\n");
    }
    return 0;
} 

#1175

#include<string.h>   
#include<queue>
#include<vector>
#include<iostream>
#include<fstream>
using namespace std;   
#define FOR(i,b,e) for(int i=(b);i<=(e);i++)  
#define FORE(i,b,e) for(int i=(b);i>=(e);i--)  
#define maxnum 100003
#define MOD 142857
int outside[maxnum];
int infect[maxnum];
struct p{
    p* next;
    int val;
    p* newest;
}*rel[maxnum];

vector<int> virus;
queue<int>  q;

int main()    
{  
    int N,M,K,A,B;
#ifdef DEBUG_
    ifstream fin("G:/1.txt");
#define cin fin
#endif
    //pre
    cin>>N>>M>>K;
    FOR(i,1,N)
        infect[i]=1;
    memset(outside,0,sizeof(outside));
    memset(rel,0,sizeof(rel));
    FOR(i,1,K){
        cin>>A;
        virus.push_back(A);
    }
    FOR(i,1,M){
        cin>>A>>B;
        outside[A]+=1;
        if(rel[B]==NULL){
            rel[B]=new p;
            rel[B]->newest=rel[B];
        }else{
            rel[B]->newest->next=new p;
            rel[B]->newest=rel[B]->newest->next;
        }
        rel[B]->newest->val=A;
        rel[B]->newest->next=NULL;
    }
    FOR(i,1,N)
        if(outside[i]==0)
            q.push(i);
    while(!q.empty()){
        A=q.front();
        p* po=rel[A];
        q.pop();
        while(po!=NULL){
            infect[po->val]=(infect[po->val]+infect[A])%MOD;
            if(--outside[po->val]==0)
                q.push(po->val);
            po=po->next;
        }
    }
    int ans=0;
    FOR(i,0,K-1)
        ans=(ans+infect[virus[i]])%MOD;

    cout<<ans<<endl;
    return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值