欧美1区2区3区激情无套,两个女人互添下身视频在线观看,久久av无码精品人妻系列,久久精品噜噜噜成人,末发育娇小性色xxxx

PAT甲級1146-Topological Order(拓撲排序一類)

一.題目

This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.

gre.jpg

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 1,000), the number of vertices in the graph, and M (≤ 10,000), the number of directed edges. Then M lines follow, each gives the start and the end vertices of an edge. The vertices are numbered from 1 to N. After the graph, there is another positive integer K (≤ 100). Then K lines of query follow, each gives a permutation of all the vertices. All the numbers in a line are separated by a space.

Output Specification:

Print in a line all the indices of queries which correspond to "NOT a topological order". The indices start from zero. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line. It is graranteed that there is at least one answer.

Sample Input:

6 8
1 2
1 3
5 2
5 4
2 3
2 6
3 4
6 4
5
1 5 2 3 6 4
5 1 2 6 3 4
5 1 2 3 6 4
5 2 1 6 3 4
1 2 3 4 5 6

Sample Output:

3 4

二.題意

首先輸入m(表示有m個數(shù))和n(有八條有向邊),接下來n行為有向圖的邊,接下來輸入k(表示有k行),每一行有m個數(shù),然后判斷是否是一個拓撲序列,將不是拓撲序列的部分輸出。

三.代碼

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n,m,a,b,k,flag=0,in[1010]={0};
    vector<int> vec[1010];//類似于二維的數(shù)組(kv型)
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>a>>b;
        vec[a].push_back(b);//將kv鍵值對push進去
        in[b]++;//in數(shù)組表示入度
    }
    cin>>k;
    for(int i=0;i<k;i++){
        int judge=1;
        vector<int> tin(in,in+n+1);//每次循環(huán)將in部分復(fù)制到tin中,in+n+1
        for(int j=0;j<n;j++){
            cin>>a;
            if(tin[a]!=0)
                judge=0;//用于判斷是不是拓撲排序
            for(int it:vec[a])
                tin[it]--;//將對應(yīng)元素的入度減一
        }
        if(judge==1)
            continue;//continue關(guān)鍵字可以進行下一次的循環(huán)
        printf("%s%d",flag==1?" ":"",i);//輸出
        flag=1;//用于判斷是不是第一個
    }
    return 0;
}

四.類似題目

1.Leetcode 207

Course Schedule

There are a total of n courses you have to take, labeled from0ton-1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:[0,1]

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?

Example 1:

Input: 2, [[1,0]] 
Output: true
Explanation: There are a total of 2 courses to take. 
             To take course 1 you should have finished course 0. So it is possible.

Example 2:

Input: 2, [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take. 
             To take course 1 you should have finished course 0, and to take course 0 you should
             also have finished course 1. So it is impossible.

Note:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.
class Solution {
public:
    bool canFinish(int n, vector<vector<int>>& pre) {
        vector<vector<int>> adj(n, vector<int>());
        vector<int> degree(n, 0);
        for (auto &p: pre) {
            adj[p[1]].push_back(p[0]);
            degree[p[0]]++;
        }
        queue<int> q;
        for (int i = 0; i < n; i++)
            if (degree[i] == 0) q.push(i);
        while (!q.empty()) {
            int curr = q.front(); q.pop(); n--;
            for (auto next: adj[curr])
                if (--degree[next] == 0) q.push(next);
        }
        return n == 0;
    }
};
全部評論

相關(guān)推薦

完美的潛伏者許愿簡歷通過:我上表jd,請求封我做后端大將軍的事,北京有消息了:竟然不許?。?! 他們一定是看我沒有實習(xí),這才故意駁回我的請求!
點贊 評論 收藏
分享
04-21 11:22
已編輯
中華女子學(xué)院 UE4
點贊 評論 收藏
分享
評論
點贊
收藏
分享

創(chuàng)作者周榜

更多
??途W(wǎng)
??推髽I(yè)服務(wù)