9.8 PAT 甲级 代码(赛后重制)

1148 Werewolf - Simple Version(20 分)

Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game,

  • player #1 said: "Player #2 is a werewolf.";
  • player #2 said: "Player #3 is a human.";
  • player #3 said: "Player #4 is a werewolf.";
  • player #4 said: "Player #5 is a human."; and
  • player #5 said: "Player #4 is a human.".

Given that there were 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liers. Can you point out the werewolves?

Now you are asked to solve a harder version of this problem: given that there were N players, with 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liers. You are supposed to point out the werewolves.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (5≤N≤100). Then N lines follow and the i-th line gives the statement of the i-th player (1≤i≤N), which is represented by the index of the player with a positive sign for a human and a negative sign for a werewolf.

Output Specification:

If a solution exists, print in a line in ascending order the indices of the two werewolves. The numbers must be separated by exactly one space with no extra spaces at the beginning or the end of the line. If there are more than one solution, you must output the smallest solution sequence -- that is, for two sequences A=a[1],...,a[M] and B=b[1],...,b[M], if there exists 0≤k<M such that a[i]=b[i] (i≤k) and a[k+1]<b[k+1], then A is said to be smaller than B. In case there is no solution, simply print No Solution.

Sample Input 1:

5
-2
+3
-4
+5
+4

Sample Output 1:

1 4

Sample Input 2:

6
+6
+3
+1
-5
-2
+4

Sample Output 2 (the solution is not unique):

1 5

Sample Input 3:

5
-2
-3
-4
-5
-1

Sample Output 3:

No Solution
# include <iostream>
# include <cmath>
# include <vector>
using namespace std;

int main(){
//	freopen("C:\\1.txt", "r", stdin);
	int n;
	scanf("%d", &n);
	vector<int> v(n+1);
	for(int i = 1; i <= n; i++) scanf("%d", &v[i]);
	for(int i = 1; i <= n; i++){  // 枚举狼人 
		for(int j = i + 1; j <= n; j++){
			vector<int> judge(n+1, 1), liar; // judge 狼人为 -1; 人为 1; 
			judge[i] = judge[j] = -1;
			for(int k = 1; k <= n; k++){  // 查找说谎者 
				if(judge[abs(v[k])] * v[k] < 0) liar.push_back(k); // 负负得正,正正为正,为真;正负得负,为假 
			}
			if(liar.size() == 2 && judge[abs(liar[0])] + judge[abs(liar[1])] == 0){ // 两个说谎者 && 一个狼人 一个人 
				printf("%d %d\n", i, j);
				return 0;
			}
		}
	}
	cout << "No Solution" << endl;
} 

 

1149 Dangerous Goods Packaging(25 分)

When shipping goods with containers, we have to be careful not to pack some incompatible goods into the same container, or we might get ourselves in serious trouble. For example, oxidizing agent (氧化剂) must not be packed with flammable liquid (易燃液体), or it can cause explosion.

Now you are given a long list of incompatible goods, and several lists of goods to be shipped. You are supposed to tell if all the goods in a list can be packed into the same container.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: N (≤10​4​​), the number of pairs of incompatible goods, and M (≤100), the number of lists of goods to be shipped.

Then two blocks follow. The first block contains N pairs of incompatible goods, each pair occupies a line; and the second one contains M lists of goods to be shipped, each list occupies a line in the following format:

K G[1] G[2] ... G[K]

where K (≤1,000) is the number of goods and G[i]'s are the IDs of the goods. To make it simple, each good is represented by a 5-digit ID number. All the numbers in a line are separated by spaces.

Output Specification:

For each shipping list, print in a line Yes if there are no incompatible goods in the list, or No if not.

Sample Input:

6 3
20001 20002
20003 20004
20005 20006
20003 20001
20005 20004
20004 20006
4 00001 20004 00002 20003
5 98823 20002 20003 20006 10010
3 12345 67890 23333

Sample Output:

No
Yes
Yes
# include <iostream>
# include <vector>
# include <set>
# include <algorithm>
using namespace std;
const int maxn = 1e6 + 10;
vector<vector<int> > v(maxn);

int main(){
//	freopen("C:\\1.txt", "r", stdin);
	int n, m;
	scanf("%d %d", &n, &m);
	for(int i = 0, a, b; i < n; i++){
		scanf("%d %d", &a, &b);
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for(int i = 0; i < m; i++){
		int numcase, flag = 1;
		bool judge[maxn] = {false};
		scanf("%d", &numcase);
		vector<int> a(numcase + 1);
		for(int j = 0; j < numcase; j++){
			scanf("%d", &a[j]);
			judge[a[j]] = true;
		}
		for(int j = 0; j < numcase; j++){
			for(int k = 0; k < v[a[j]].size(); k++){
				if(judge[v[a[j]][k]] == 1){
					flag = 0;
				}
			}
		}
		if(flag || numcase == 0) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
} 

 

1150 Travelling Salesman Problem(25 分)

The "travelling salesman problem" asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city?" It is an NP-hard problem in combinatorial optimization, important in operations research and theoretical computer science. (Quoted from "https://en.wikipedia.org/wiki/Travelling_salesman_problem".)

In this problem, you are supposed to find, from a given list of cycles, the one that is the closest to the solution of a travelling salesman problem.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<N≤200), the number of cities, and M, the number of edges in an undirected graph. Then M lines follow, each describes an edge in the format City1 City2 Dist, where the cities are numbered from 1 to N and the distance Dist is positive and is no more than 100. The next line gives a positive integer K which is the number of paths, followed by K lines of paths, each in the format:

n C​1​​ C​2​​ ... C​n​​

where n is the number of cities in the list, and C​i​​'s are the cities on a path.

Output Specification:

For each path, print in a line Path X: TotalDist (Description) where X is the index (starting from 1) of that path, TotalDist its total distance (if this distance does not exist, output NA instead), and Description is one of the following:

  • TS simple cycle if it is a simple cycle that visits every city;
  • TS cycle if it is a cycle that visits every city, but not a simple cycle;
  • Not a TS cycle if it is NOT a cycle that visits every city.

Finally print in a line Shortest Dist(X) = TotalDist where X is the index of the cycle that is the closest to the solution of a travelling salesman problem, and TotalDist is its total distance. It is guaranteed that such a solution is unique.

Sample Input:

6 10
6 2 1
3 4 1
1 5 1
2 5 1
3 1 8
4 1 6
1 6 1
6 3 1
1 2 1
4 5 1
7
7 5 1 4 3 6 2 5
7 6 1 3 4 5 2 6
6 5 1 4 3 6 2
9 6 2 1 6 3 4 5 2 6
4 1 2 5 1
7 6 1 2 5 4 3 1
7 6 3 2 5 4 1 6

Sample Output:

Path 1: 11 (TS simple cycle)
Path 2: 13 (TS simple cycle)
Path 3: 10 (Not a TS cycle)
Path 4: 8 (TS cycle)
Path 5: 3 (Not a TS cycle)
Path 6: 13 (Not a TS cycle)
Path 7: NA (Not a TS cycle)
Shortest Dist(4) = 8

 

# include <iostream>
# include <vector>
# include <set>
# include <algorithm>
using namespace std;
const int maxn = 222;
const int inf = 1e9;
int G[maxn][maxn];

int main(){
//	freopen("C:\\1.txt", "r", stdin);
	int n, m, numcase, minlen = inf, index;
	fill(G[0], G[0] + maxn * maxn, 0);
	scanf("%d %d", &n, &m);
	for(int i = 0; i < m; i++){
		int a, b, c;
		scanf("%d %d %d", &a, &b, &c);
		G[a][b] = G[b][a] = c;
	}
	scanf("%d", &numcase);
	for(int i = 1; i <= numcase; i++){
		int nump, flag = 1, pathlen = 0;
		scanf("%d", &nump);
		vector<int> v(nump);
		set<int> s;
		for(int j = 0; j < nump; j++){
			scanf("%d", &v[j]);
			s.insert(v[j]);
		}
		for(int j = 1; j < v.size(); j++){
			if(G[v[j-1]][v[j]] == 0) flag = 0;
			pathlen += G[v[j-1]][v[j]];
		}
		if(flag == 0) printf("Path %d: NA (Not a TS cycle)\n", i);
		else if(v[0] != v[v.size() -1] || s.size() < n) printf("Path %d: %d (Not a TS cycle)\n", i, pathlen);
		else if(nump > n+1){
			printf("Path %d: %d (TS cycle)\n", i, pathlen);
			if(pathlen < minlen){
				minlen = pathlen;
				index = i;
			}
		}
		else{
			printf("Path %d: %d (TS simple cycle)\n", i, pathlen);
			if(pathlen < minlen){
				minlen = pathlen;
				index = i;
			}
		}
	}
	printf("Shortest Dist(%d) = %d\n", index, minlen);
}

 

1151 LCA in a Binary Tree(30 分)

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.

Given any two nodes in a binary tree, you are supposed to find their LCA.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.

Output Specification:

For each given pair of U and V, print in a line LCA of U and V is A. if the LCA is found and A is the key. But if A is one of U and V, print X is an ancestor of Y. where X is A and Y is the other node. If U or V is not found in the binary tree, print in a line ERROR: U is not found. or ERROR: V is not found. or ERROR: U and V are not found..

Sample Input:

6 8
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99

Sample Output:

LCA of 2 and 6 is 3.
8 is an ancestor of 1.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.
# include <iostream>
# include <vector>
# include <map>
const int maxn = 1e5;
using namespace std;
vector<int> in, pre;
map<int, int> pos;

void LCA(int inl, int inr, int preroot, int a, int b){
	if(inl > inr) return ;
	int inroot = pos[pre[preroot]], ain = pos[a], bin = pos[b];
	if(ain < inroot && bin < inroot)
		LCA(inl, inroot -1, preroot +1, a, b);
	else if((ain < inroot && bin > inroot) || (ain > inroot && bin < inroot))
		printf("LCA of %d and %d is %d.\n", a, b, in[inroot]);
	else if(ain > inroot && bin > inroot)
		LCA(inroot +1, inr, preroot +1 +(inroot - inl), a, b);
	else if(ain == inroot)
		printf("%d is an ancestor of %d.\n", a, b);
	else if(bin == inroot)
		printf("%d is an ancestor of %d.\n", b, a);
}
/*
void LCA(int inl, int inr, int preRoot, int a, int b) {
    if (inl > inr) return;
    int inRoot = pos[pre[preRoot]], aIn = pos[a], bIn = pos[b];
    if (aIn < inRoot && bIn < inRoot)
        LCA(inl, inRoot-1, preRoot+1, a, b);
    else if ((aIn < inRoot && bIn > inRoot) || (aIn > inRoot && bIn < inRoot))
        printf("LCA of %d and %d is %d.\n", a, b, in[inRoot]);
    else if (aIn > inRoot && bIn > inRoot)
        LCA(inRoot+1, inr, preRoot+1+(inRoot-inl), a, b);
    else if (aIn == inRoot)
            printf("%d is an ancestor of %d.\n", a, b);
    else if (bIn == inRoot)
            printf("%d is an ancestor of %d.\n", b, a);
}*/

int main(){
//	freopen("C:\\1.txt", "r", stdin);
	int n, m;
	scanf("%d %d", &n, &m);
	in.resize(m + 1);
	pre.reserve(m + 1);
	for(int i = 1; i <= m; i++){
		scanf("%d", &in[i]);
		pos[in[i]] = i;
	}
	for(int i = 1; i <= m; i++){
		scanf("%d", &pre[i]);
	}
	for(int i = 0; i < n; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		if(pos[a] == 0 && pos[b] == 0)
			printf("ERROR: %d and %d are not found.\n", a, b);
		else if(pos[a] == 0 || pos[b] == 0)
			printf("ERROR: %d is not found.\n", pos[a]?b:a);
		else LCA(1, m, 1, a, b);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值