这个题应该是个优先队列的模版题 当时比赛的时候没时间做先贴一下大神的代码好好学习学习

B - Gandalf vs the Balrog

Time Limit:2000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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.
Time Limit: 4s
Memory Limit: 64MB
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.

'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.

 #include <iostream>

 using namespace std;

 int array[];

 int main()
{
int t, n, m, x, y;
cin >> t; while (t--)
{
cin >> n >> m; for (int i = ; i <= n; i++)
{
array[i] = n - i;
} for (int i = ; i < m; i++)
{
cin >> x >> y;
array[x]--;
array[y]++;
} int ok = ;
for (int i = ; i <= n; i++)
{
if (array[i] == )
{
ok = i;
break;
}
} if (ok)
{
cout << << ' ' << ok << endl;
}
else
{
cout << << endl;
}
}
return ;
}
 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<iostream>
#include<vector>
#define N 1001000 using namespace std; int in[N];
int out[N];
int n,m;
int main()
{
int cas;
scanf("%d",&cas);
while (cas--){
scanf("%d%d",&n,&m);
memset(in,,sizeof(in));
memset(out,,sizeof(out)); for (int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
in[y]++;
out[x]++;
}
int i = n;
while (i>= && !(in[i]== && out[i]>=(n - i))){
i--; }
if (i>=){
printf("2 %d\n",i);
}else{
if (m == (n-)*n /)
printf("0\n");
else
printf("1\n");
}
}
return ;
}
 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <climits>
#include <ctime>
#include <vector>
#include <set>
#include <stack>
#include <sstream>
#include <iomanip>
#define MAX 1000010
#define CLR(arr,val) memset(arr,val,sizeof(arr)) using namespace std; int cards[MAX], defeated[MAX]; int main()
{
std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen( "in.txt", "r", stdin );
//freopen( "out.txt", "w", stdout );
clock_t program_start, program_end;
program_start = clock();
#endif
int T, N, M, x, y, max_card;
cin >> T;
while ( T-- )
{
cin >> N >> M;
for ( int i = ; i <= N; ++i )
{
cards[i] = ;
defeated[i] = ;
}
for ( int i = ; i < M; ++i )
{
cin >> x >> y;
cards[x]++; defeated[y]++;
}
max_card = -;
for ( int i = N; i >= ; --i )
if ( cards[i] == N - i && defeated[i] == )
{
max_card = i;
break;
}
if ( max_card != - )
cout << "2 " << max_card << endl;
else
cout << "" << endl;
} #ifndef ONLINE_JUDGE
program_end = clock();
cerr << "Time consumed: " << endl << ( program_end - program_start ) << " MS" << endl;
#endif
}

SPOJ AMR12B 720的更多相关文章

  1. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  2. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  3. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  4. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  5. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  6. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  7. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  8. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...

  9. SPOJ GSS2 Can you answer these queries II

    Time Limit: 1000MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description Being a ...

随机推荐

  1. Dubbo相关的基础

    Dubbo是一款高性能轻量级的java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务注册与发现. Dubbo是阿里开源的一个项目,现在已经是Apache的顶级 ...

  2. 日志(logging)与正则(re)模块

    logging模块 #日志:日常的流水 =>日志文件,将程序运行过程中的状态或数据进行记录,一般都是记录到日志文件中 #1.logging模块一共分为五个打印级别 debug.info.warn ...

  3. 请求上下文HttpContext解释

    1 HttpContext上下文作用 有关应用程序状态信息,处理的请求以及构建的响应等信息全部通过HttpContext上下文获取 2 Httpcontext类用于从头至尾跟踪请求的状态,他也是有关请 ...

  4. CRM WebClient UI的浏览器打印实现

    WebClient UI上自带了一个打印按钮,按Ctrl + P后可以生成一个新的页面供打印. 如下图所示.可以看到这个页面里所有的超链接都已经被移除了. 这个页面的生成逻辑如下. 1. 按住ctrl ...

  5. 9.动态SQL

    动态 SQL,主要用于解决查询条件不确定的情况:在程序运行期间,根据用户提交的查 询条件进行查询. 提交的查询条件不同,执行的 SQL 语句不同.若将每种可能的情况均逐一 列出,对所有条件进行排列组合 ...

  6. 第五章、Django之模型层---单表操作

    目录 第五章.Django之模型层---单表操作 一.ORM查询 二.Django测试环境搭建 三.单表查询 1. 增 2. 改 3. 删 4. 查 第五章.Django之模型层---单表操作 一.O ...

  7. 智能指针原理及实现(1)shared_ptr

    0.异常安全 C++没有内存回收机制,每次程序员new出来的对象需要手动delete,流程复杂时可能会漏掉delete,导致内存泄漏.于是C++引入智能指针,可用于动态资源管理,资源即对象的管理策略. ...

  8. CentOS 6.10 系统安装

    本章内容: CentOS 6.10 的安装 一.安装光盘,选择 Install or upgrade an existing system 二.选择 skip 跳过光盘检查 三.选择 Next 四.选 ...

  9. Linux磁盘及文件系统管理4

    文件系统的使用: 首先要“挂载”:mount命令和umount命令 根据文件系统之外的其它文件系统要想能够被访问,都必须通过“关联”到根文件系统上的某个目录来实现,此关联操作即为“挂载”:此目录即为“ ...

  10. SQL Server 排序规则的影响

    目录 SQL Server 排序规则 影响 效果演示 更改数据库排序规则 服务器级排序规则 数据库级排序规则 列级排序规则 查询时指定规则 建议 使用 Unicode 数据类型 使用二进制排序规则 [ ...