PAT (Advanced Level) Practice - 1146 Topological Order(25 分)

简介: PAT (Advanced Level) Practice - 1146 Topological Order(25 分)

题目链接:点击打开链接

题目大意:判断以下序列是否符合拓扑排序定义,若不符合,输出第 i-th 的序列对应的 i。

解题思路:模拟 Topo 排序。

AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a)
#define ssclr(ss) ss.clear(), ss.str("")
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
const int NV=1e3+10;
int n;
int in[NV], tin[NV];
int mp[NV][NV];
set<int> st;
void init()
{
    st.clear();
    for(int i=1;i<=n;i++)
    {
        tin[i]=in[i];
        if(in[i]==0)
        {
            st.insert(i);
        }
    }
}
int main()
{
    int m,q,a,u,v,first=1;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&u,&v);
        if(!mp[u][v])
        {
            mp[u][v]=1;
            in[v]++;
        }
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++)
    {
        int f=1;
        init();
        for(int j=0;j<n;j++)
        {
            scanf("%d",&a);
            auto it=st.find(a);
            if(it!=st.end())
            {
                st.erase(it);
                for(int k=1;k<=n;k++)
                {
                    if(mp[a][k]==1)
                    {
                        tin[k]--;
                        if(tin[k]==0) st.insert(k);
                    }
                }
            }
            else f=0;
        }
        if(!f)
            if(first) first=0, printf("%d",i);
            else printf(" %d",i);
    }
    puts("");
    return 0;
}
目录
相关文章
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
100 0
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
133 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
124 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
134 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
141 0
PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)
PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)
115 0
PAT (Advanced Level) Practice - 1012 The Best Rank(25 分)
PAT (Advanced Level) Practice - 1012 The Best Rank(25 分)
120 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
145 0
PAT (Advanced Level) Practice - 1111 Online Map(30 分)
PAT (Advanced Level) Practice - 1111 Online Map(30 分)
119 0
|
存储 测试技术
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
115 0