Hangzhou Electric acm2094--Produce Champions ~ ~ Topological sorting

Source: Internet
Author: User
Tags strcmp

The meaning of the subject, title. It's easy to understand.

The solution is to sort the topology, which can be easily solved.

Each input pair of players, to determine whether two players have appeared, did not appear, create a new head node, added to the adjacency table, update the node in the degree.

Finally determine if there is a node in the degree of 0, there is, yes, otherwise no.

I'm using the list container in the STL to create the adjacency table.

The following is the code for the AC:

#include <iostream> #include <list> #include <cstring>using namespace Std;class data//node structure { Public:int indegree;int Point;char str[100];};    List <data> list[2000];  Linked list array, existence of multiple people int main () {int n, I, J, K;char s1[100], s2[100];d ata temp;while (CIN >> n && N) {for (i = 0; i < 2000; i++)//clear each linked list list[i].clear (); k = 0;for (i = 0; i < n; i++) {cin >> s1 >> s2;int flag = 0, Tag = 0, a, B;a =                                b = -1;for (j = 0; J < K; J + +) {if (strcmp (List[j].front (). str, s1) = = 0)//Determine if the first person has ever appeared {a = J; Flag flag in List of linked list flags = 1;}                                 if (strcmp (List[j].front (). str, s2) = = 0)//Determine if the second person has appeared {b = j; Ibid. tag = 1;;}} if (!flag)//does not exist, create a new one, enlarge the size of the array {strcpy (temp.str, s1); temp.indegree = 0;temp.point =-1; List[k].insert (List[k].end (), temp); k++;} if (!tag)//Ibid. {strcpy (temp.str, s2); temp.indegree = 0;temp.point =-1; List[k].insert (List[k].end (), temp); k++;} if (Flag &&!tag)//first appears, the second does not have {List[k-1].front (). indegree++; Update into strcpy (TEMP.STR, s2); temp.indegree = -1;temp.point = k-1; List[a].insert (List[a].end (), temp);} else if (!flag && tag)//The first one does not appear, the second one appears {List[b].front (). indegree++;strcpy (TEMP.STR, s2); temp.indegree =-1; Temp.point = b; List[k-1].insert (List[k-1].end (), temp);} else if (flag && tag)//both appear {List[b].front (). indegree++;strcpy (TEMP.STR, s2); temp.indegree = -1;temp.point = b; List[a].insert (List[a].end (), temp);} else if (!flag &&!tag)//Neither appears {List[k-1].front (). indegree++;strcpy (TEMP.STR, s2); temp.indegree =-1; Temp.point = k-1; List[k-2].insert (List[k-2].end (), temp);}} int ans = 0;for (i = 0; i < K; i++) {if (List[i].front (). Indegree = = 0 &&! List[i].empty ()) ans++;} if (ans = = 1) cout << "Yes" << endl;elsecout << "No" << Endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hangzhou Electric acm2094--Produce Champions ~ ~ Topological sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.