The problem of two topological sorts

Source: Internet
Author: User
Tags in degrees sorts

How long has not written the thing ah ....

Two topological sorting Liv.1 .... The method is the same ~ ~

"Topology sequencing • Two"

Title: HTTP://HIHOCODER.COM/CONTEST/HIHO81/PROBLEM/1

A computer network, one-way edge, if there is an edge u->v, then the virus of u will infect v.

Point, there is no ring! Then if you =0, then the number of viruses in u will not change.

Think of the topology sort. Continue to delete the point of 0 in degrees. Each time you delete node U, if there is u->v, then the number of viruses

NUM[V] + = Num[u]. Problem solving.

(using queue to achieve topological ordering is very convenient ~ ~)

Code:

1 #include <bits/stdc++.h>23UsingNamespaceStd4Constint MAXN =100000 +10;5Constint mod =142857;6IntN, M, K;7int NUM[MAXN],Inch[MAXN];8 vector<Int>G[MAXN];910IntSolve () {queue<Int>Q1213int ans =0;14Forint i =1; I <= N; ++i) {15if (!Inch[i]) {16//cout << "I:" << i << Endl;17Q.push (i);18}19}2021stwhile (!Q.empty ()) {22int u =Q.front (); Q.pop ();% ans = (ans + num[u])%MoD2425Forint i =0; I < g[u].size (); ++i) {26int v =G[u][i];27in[v]--; NUM[V] = (Num[u] + num[v])%MoD28if (!Inch[v])29Q.push (v);30}31}3233ReturnAns34}3536IntMain () {Panax Notoginseng scanf ("%d%d%d", &n, &m, &k);38The memset (NUM,0,sizeof(num));40IntU, v;41Forint i =0; I < K; ++i) {scanf ("%d", &u);num[u]++;44}45memset (In0,sizeofInch) );47Forint i =0; I < m; ++i) {scanf ("%d%dv) ; 49 G[u].push_back (v); in[v]++; }51 52 int ans = solve (); %d\n" , ans); 54 55 return 0; *                 
View Code

"Hiho for the 81st Week"

Title: HTTP://HIHOCODER.COM/CONTEST/HIHO81/PROBLEM/1

method is the same, the map is slightly difficult ...

Code:

1 #include <bits/stdc++.h>23UsingNamespaceStd4Constint MAXN =100000 +10;5Constint mod =142857;6IntN, M;7 vector<Int>Inch[MAXN];8IntS[MAXN], RU[MAXN], NUM[MAXN];9 vector<Int>E[MAXN], G[MAXN];1011voidInit () {12Forint i =0; i < MAXN; ++I13Inch[I].clear (), E[i].clear (), g[i].clear ();memset (s),0,sizeof(s));memset (Ru,0,sizeof(RU));memset (NUM,0,sizeof(num));17}1819voidSolve () {queue<Int>Q21stForint i =0; I <= N; ++i) {22if (!Ru[i])23Q.push (i);24}2526while (!Q.empty ()) {27int u =Q.front (); Q.pop ();28Forint i =0; I < g[u].size (); ++i) {29int v =G[u][i];RU[V]--, Num[v] = (Num[v] + num[u])%MoD31if (!RU[V])32Q.push (v);33}34}35}3637voidPrint () {* printf ("%d", num[1]);39Forint i =2; I <= N; ++i) {printf ("%d", Num[i]);41}printf ("\ n");43}4445IntMain () {46//Freopen ("1.in", "R", stdin);47IntTscanf ("%d", &T);4950while (t--){51Init ();scanf ("%d%d", &n, &m);5354IntU, V, K;s[0] =-1, num[0] =1;56Forint i =0; I < m; ++i) {scanf ("%d", &u);e[0].push_back (U);59}6061Forint i =1; I <= N; ++i) {scanf ("%d%d", &u, &k);S[i] = u;Inch[U].push_back (i);64ForInt J =0; J < K; ++j) {scanf ("%d", &u);66E[i].push_back (U);67}68}6970//Build graph71Forint i =0; I <= N; ++i) {72ForInt J =0; J < E[i].size (); ++j) {73int sg =E[I][J];74Forint p =0; P <In[sg].size (); ++P) {75int v =IN[SG][P]; ru[v]++;76  G[i].push_back (v) }78 }79 }80 //output (); 81 82  solve (); 83  print (); }85 86 return 0;                
View Code

The problem of two topological sorts

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.