Link: poke here
Gym Class time limit:6000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Problem Description
As we all know, the degree bears like all kinds of sports activities.
Today, it finally became the dream of the physical education teacher. For the first time in class, it found an interesting thing. Before class, all the classmates to be lined up, assuming that at first everyone has a unique ID, from 1 to N, after the row, each student will find the minimum ID of all the students in front of them, including themselves, as a score of their own evaluation of this class. Trouble is, some students do not want to some (some) classmates in front of him (her), in the case of satisfying this premise, the new PE teacher-degree bear, hope that the final line results can make all students evaluation scores and maximum.
Input
The first line is an integer t, which represents the T (1≤t≤30) group data.
For each set of data, the first line enters two integers N and M (1≤n≤100000,0≤m≤100000), each representing the total number of people and the preferences of some students.
The next m line, two integers a and B (1≤a,b≤n) per line, indicates that the classmate with ID A does not want the classmate with ID B to rank before him or her. You can assume that the title guarantee has at least one arrangement that meets all requirements.
Output
For each set of data, the maximum score is output.
Sample Input
3
1 0
2 1
1 2
3 1
3 1
Sample Output
1
2
6
Ideas:
The simple picture below shows the topological order, then max at a point of 0 for each time, and then joins the new 0 point into the priority queue
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < string> #include <vector> #include <ctime> #include <queue> #include <set> #include <map > #include <stack> #include <iomanip> #include <cmath> #define MST (SS,B) memset ((ss), (b), sizeof (SS )) #define MAXN 0x3f3f3f3f #define MAX 1000100///#pragma comment (linker, "/stack:102400000,102400000") typedef long LONG
ll
typedef unsigned long long ull;
#define INF (1LL<<60)-1 using namespace std;
struct node{int val,deep;
BOOL operator < (const node &a) const{return val<a.val;
}}s[100100]; struct edge{int v,next;}
E[500100];
int N,m,tot;
int head[100100],vis[100100];
void Init () {MST (head,-1);
tot=0;
} void Add (int u,int v) {e[tot].v=v;
E[tot].next=head[u];
head[u]=tot++;
} int anw[100100];
void Solve () {int cnt=0;
Priority_queue<node> Qu; for (int i=1;i<=n;i++) {
if (s[i].deep==0) {Qu.push (s[i]);
}} ll ans=0;
while (!qu.empty ()) {node now=qu.top ();
Qu.pop ();
Anw[++cnt]=now.val;
for (int i=head[now.val];i!=-1;i=e[i].next) {int v=e[i].v;
s[v].deep--;
if (s[v].deep==0) Qu.push (S[v]);
}} int mn=n;
for (int i=1;i<=n;i++) {mn=min (mn,anw[i]);
ans+= (LL) mn;
} printf ("%i64d\n", ans);
} int main () {int T;
scanf ("%d", &t);
while (t--) {init ();
scanf ("%d%d", &n,&m);
for (int i=1;i<=n;i++) {s[i].val=i;s[i].deep=0;
} for (int i=1;i<=m;i++) {int u,v;
scanf ("%d%d", &u,&v);
ADD (U,V);
s[v].deep++;
} solve ();
} return 0; }