SPOJ AMR12B 720
这个题应该是个优先队列的模版题 当时比赛的时候没时间做先贴一下大神的代码好好学习学习
Description
'We fought far under the living earth, where time is not counted. Ever he clutched me, and ever I hewed him, till at last he fled into dark tunnels. Ever up now we went, until we came to the Endless Stair. Out he sprang, and even as I came behind, he burst into new flame. Those that looked up from afar thought that the mountain was crowned with storm. Thunder they heard, and lightning, they said, smote upon Celebdil, and leaped back broken into tongues of fire.' - Gandalf, describing his fight against the Balrog.
Although Gandalf would not go into the details of his battle, they can be summarized into the following simplified form: both Gandalf and the Balrog have a set of N attacks they can use (spells, swords, brute-force strength etc.). These attacks are numbered from 1 to N in increasing order of Power. When each has chosen an attack, in general, the one with the higher power wins. However, there are a few ("M") anomalous pairs of attacks, in which the lesser-powered attack wins.
Initially, Gandalf picks an attack. Then the Balrog counters it with one of the remaining attacks. If the Balrog's counter does not defeat Gandalf's, then we say Gandalf receives a score of 2. If however it does, then Gandalf has exactly one more opportunity to pick an attack that will defeat the Balrog's. If he manages to defeat him now, his score will be 1, whereas if he is still unable to defeat him, his score will be 0.
Your task is to determine, given N and the M anomalous pairs of attacks, what will be Gandalf's score, given that both play optimally. Further, in case Gandalf gets a score of 2, you must also determine which attack he could have chosen as his first choice.
Note 1: The Balrog can choose only one attack, whereas Gandalf can choose upto two.
Note 2: The relation A defeats B is not transitive within the attacks. For example, attack A can defeat attack B, attack B can defeat attack C, and attack C can defeat attack A.
Note 3: Between any two attacks A and B, either attack A defeats attack B or attack B defeats attack A.
Input (STDIN):
The first line will consist of the integer T, the number of test-cases.
Each test case begins with a single line containing two integers N and M.
This is followed by M lines consisting of 2 integers each x and y, denoting that x and y are an anomalous pair.
Output (STDOUT):
For each test-case, output a single line either
2 A, if Gandalf can defeat any attack the Balrog chooses if he picks attack A,
1, if Gandalf can choose an attack such that even if the Balrog chooses an attack to defeat him, he can choose an attack to defeat the Balrog's chosen card,
0, if none of the above two options are possible for all possible choices of Gandalf's attack(s).
Between successive test cases, there should not be any blank lines in the output.
Constraints:
1 <= T <= 15
3 <= N <= 1,000,000
0 <= M <= min(N(N-1)/2, 300,000)
1 <= x < y <= N for all the anomalous pairs (x,y)
The sum of M over all test-cases will not exceed 300,000.
Sample Input:
2
3 0
3 1
1 3
Sample Output:
2 3
1
Notes/Explanation of Sample Input:
In the first case, attack 3 can beat both attacks 1 and 2. So Gandalf just chooses attack 3.
In the second case, attack 1 beats 3 which beats 2 which beats 1. No matter which attack Gandalf chooses, the Balrog can pick the one which defeats his, but then he can pick the remaining attack and defeat the Balrog's.
1 #include <iostream> 2 3 using namespace std; 4 5 int array[1000001]; 6 7 int main() 8 { 9 int t, n, m, x, y; 10 cin >> t; 11 12 while (t--) 13 { 14 cin >> n >> m; 15 16 for (int i = 1; i <= n; i++) 17 { 18 array[i] = n - i; 19 } 20 21 for (int i = 0; i < m; i++) 22 { 23 cin >> x >> y; 24 array[x]--; 25 array[y]++; 26 } 27 28 int ok = 0; 29 for (int i = 1; i <= n; i++) 30 { 31 if (array[i] == 0) 32 { 33 ok = i; 34 break; 35 } 36 } 37 38 if (ok) 39 { 40 cout << 2 << ' ' << ok << endl; 41 } 42 else 43 { 44 cout << 1 << endl; 45 } 46 } 47 return 0; 48 }
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<algorithm> 5 #include<string> 6 #include<iostream> 7 #include<vector> 8 #define N 1001000 9 10 using namespace std; 11 12 int in[N]; 13 int out[N]; 14 int n,m; 15 int main() 16 { 17 int cas; 18 scanf("%d",&cas); 19 while (cas--){ 20 scanf("%d%d",&n,&m); 21 memset(in,0,sizeof(in)); 22 memset(out,0,sizeof(out)); 23 24 for (int i=1;i<=m;i++){ 25 int x,y; 26 scanf("%d%d",&x,&y); 27 in[y]++; 28 out[x]++; 29 } 30 int i = n; 31 while (i>=1 && !(in[i]==0 && out[i]>=(n - i))){ 32 i--; 33 34 } 35 if (i>=1){ 36 printf("2 %d\n",i); 37 }else{ 38 if (m == (n-1)*n /2) 39 printf("0\n"); 40 else 41 printf("1\n"); 42 } 43 } 44 return 0; 45 }
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cmath> 6 #include <cstring> 7 #include <cctype> 8 #include <climits> 9 #include <ctime> 10 #include <vector> 11 #include <set> 12 #include <stack> 13 #include <sstream> 14 #include <iomanip> 15 #define MAX 1000010 16 #define CLR(arr,val) memset(arr,val,sizeof(arr)) 17 18 using namespace std; 19 20 int cards[MAX], defeated[MAX]; 21 22 int main() 23 { 24 std::ios::sync_with_stdio(false); 25 #ifndef ONLINE_JUDGE 26 freopen( "in.txt", "r", stdin ); 27 //freopen( "out.txt", "w", stdout ); 28 clock_t program_start, program_end; 29 program_start = clock(); 30 #endif 31 int T, N, M, x, y, max_card; 32 cin >> T; 33 while ( T-- ) 34 { 35 cin >> N >> M; 36 for ( int i = 1; i <= N; ++i ) 37 { 38 cards[i] = 0; 39 defeated[i] = 0; 40 } 41 for ( int i = 0; i < M; ++i ) 42 { 43 cin >> x >> y; 44 cards[x]++; defeated[y]++; 45 } 46 max_card = -1; 47 for ( int i = N; i >= 1; --i ) 48 if ( cards[i] == N - i && defeated[i] == 0 ) 49 { 50 max_card = i; 51 break; 52 } 53 if ( max_card != -1 ) 54 cout << "2 " << max_card << endl; 55 else 56 cout << "1" << endl; 57 } 58 59 #ifndef ONLINE_JUDGE 60 program_end = clock(); 61 cerr << "Time consumed: " << endl << ( program_end - program_start ) << " MS" << endl; 62 #endif 63 }