UVa 11748: Rigging Elections

来源:互联网 发布:jo抢购软件 编辑:程序博客网 时间:2024/05/17 16:13

先附上题目如下:

Elections in your country are performed in a one-on-one elimination style. Each week, two people are chosen from a pool of candidates and the country votes on which one they prefer. The loser is eliminated and the winner is returned to the pool of candidates. This process continues until only one candidate remains.

To make a bad voting system even worse, a single person is charged with the responsibility of choosing the two candidates each week. This person happens to be you! Since you are a very selfish person, you plan on rigging the election so your preferred candidate wins. You have access to polling data from which you can determine who would win in every possible head-to-head matchup. Assuming the data accurately represents what the real outcome would be, is it possible to schedule the candidates so your candidate wins?

Input Format

The first line of each test case contains three integers n, m, and c with 1 ≤ n ≤ 100, 1 ≤ m ≤ 100 and 1 ≤ c ≤ n. Here, n indicates the total number of candidates in the initial pool, m is the number of voters and c is the number of your preferred candidate. This is followed by m lines, each containing a permutation of the numbers 1 through n. The i'th line should be interpreted as a ranking of the n candidates by voter i. If two candidates are pitted against each other in an election, then voter i will vote for whoever appears first in their list. You may also assume m is always odd. The last line of input contains three zeros and should not be processed.

Output Format

There is a single line of output for each test case with either the message yes or no indicating if it is possible for you to rig the elections so your preferred candidate c wins.

Sample Input

3 3 11 2 32 3 13 1 23 3 11 2 32 3 13 2 10 0 0

Sample Output

yesno

这道题需要我们判断所给的c号候选是否可以通过你安排的竞选顺序使之成为最终胜者,已知任意两个候选竞选的结果。

这是一道隐式的图论题,我们将n为候选看作n个节点,如果i和j号候选进行竞选,i获胜,则在节点i与j间增加一条i指向j的边。

最后看图中是否存在以c为根的生成树即可。

我使用DFS产生生成树,解题代码如下:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <string>#include <algorithm>#include <vector>using namespace std;#define max 105int Rank[max];//Rank存放某个投票者的输入,Rank[i]表示候选者i的名次int Count[max][max];//Count[i][j]存放将候选者i放在j前面的投票者人数vector<int> win[max];//win[i]表示候选者i可以胜的其他候选int vis[max];int n,m,c;void dfs(int s){vis[s] = 1;int tmp;while(!win[s].empty()){tmp = win[s].back(); win[s].pop_back();if(!vis[tmp]){dfs(tmp);}}}bool SpanningTree(int s){memset(vis,0,sizeof(vis));dfs(s);bool ok = true;for(int i=0; i<n; i++)if(!vis[i]) { ok = false; break; }return ok;}int main(){int tmp;while(cin >> n >> m >> c && n){memset(Count,0,sizeof(Count));for(int k=0; k<m; k++){for(int i=0; i<n; i++){cin >> tmp;Rank[tmp-1] = i;}for(int i=0; i<n; i++){for(int j=i+1; j<n; j++){if(Rank[i]<Rank[j]) Count[i][j]++;else if(Rank[i]>Rank[j]) Count[j][i]++;}}}for(int i=0; i<n; i++) win[i].clear();for(int i=0; i<n; i++){for(int j=i+1; j<n; j++){if(Count[i][j]>m/2) win[i].push_back(j);else win[j].push_back(i);}}if(SpanningTree(c-1)) cout << "yes\n";else cout << "no\n";}return 0;}


原创粉丝点击