week8作业题_C - 班长竞选

C - 班长竞选


题目描述

大学班级选班长,N 个同学均可以发表意见 若意见为 A B 则表示 A 认为 B 合适,意见具有传递性,即 A 认为 B 合适,B 认为 C 合适,则 A 也认为 C 合适 勤劳的 TT 收集了M条意见,想要知道最高票数,并给出一份候选人名单,即所有得票最多的同学,你能帮帮他吗?

Input
本题有多组数据。第一行 T 表示数据组数。每组数据开始有两个整数 N 和 M (2 <= n <= 5000, 0 <m <= 30000),接下来有 M 行包含两个整数 A 和 B(A != B) 表示 A 认为 B 合适。
Output
对于每组数据,第一行输出 “Case x: ”,x 表示数据的编号,从1开始,紧跟着是最高的票数。 接下来一行输出得票最多的同学的编号,用空格隔开,不忽略行末空格!
Sample Input
2
4 3
3 2
2 0
2 1

3 3
1 0
2 1
0 2
Sample Output
Case 1: 2
0 1
Case 2: 2
0 1 2

题目思路

在这个题目中,如果a给b投票,我们可以转化成在图中的一条边。这样通过不断的加边,我们就可以找出所有的强连通分量,然后对所有的强连通分量进行缩点处理。
在求SCC的时候使用的是Kosaraju算法,可以通过两遍dfs实现,第一遍求出原图中的逆后序序列,第二遍dfs在反图中按照逆后序序列进行遍历,求出SCC。
在求完SCC后,我们需要做的是缩点操作,将处在同一个SCC中的所有点看成1个点,然后缩点之后找到出度为0的点,然后从这个点在缩点反图中进行dfs遍历,找到所有可以到达这个点的其他点。这里需要注意最后的结果不止一个,有两种情况,一是当前SCC中的点,ans+=SCC[i]–1,二是其它SCC中的点SUM( SCC[j]),其中点j可到达点i。

代码实现

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define _for(i,a,b) for(int i = (a); i < (b); i++)
#define _rep(i,a,b) for(int i = (a); i <= (b); i++)
const int maxn = 5100;
const int maxm = 30100; 

int f[maxn],nf[maxn];
int vis1[maxn],vis2[maxn],scc[maxn],scc2[maxn],in_deg[maxn];
int T,n,s,mx;
int fcnt,acnt1,acnt2,acnt3,scnt;
int a,b;
int head1[maxn],head2[maxn],head3[maxn];
int ans[maxn],ans2[maxn];
 
struct edge{
	int to,next;
}e1[maxm],e2[maxm],e3[maxn];


void inia()
{
	fcnt=-1;
	acnt1=0;
	acnt2=0;
	acnt3=0;
	scnt=0;
	
	memset(ans,0,sizeof(ans));
	memset(ans2,0,sizeof(ans2));
	memset(f,0,sizeof(f));
	memset(nf,0,sizeof(nf));
	memset(vis1,0,sizeof(vis1));
	memset(scc,0,sizeof(scc));
	memset(scc2,0,sizeof(scc2));
	memset(in_deg,0,sizeof(in_deg));
	memset(head1,-1,sizeof(head1));
	memset(head2,-1,sizeof(head2));
	memset(head3,-1,sizeof(head3));
} 

void addedge(int x,int y,int cs)
{
	if(cs == 1)
	{
		e1[acnt1].to =y;
		e1[acnt1].next =head1[x];
		head1[x] = acnt1;
  		acnt1++;
	}
	else if(cs == 2)
	{
		e2[acnt2].to = y;
		e2[acnt2].next = head2[x];
		head2[x] = acnt2;
 		acnt2++;
	}
	else
	{
		e3[acnt3].to = y;
		e3[acnt3].next = head3[x];
		head3[x] = acnt3;
   		acnt3++;
      
  		in_deg[y]++;
	}
	
}


void dfs1(int s)
{
	vis1[s] =1 ;
	for(int i = head1[s]; i != -1;i = e1[i].next)
	{
		int v = e1[i].to;
		if(!vis1[v])
		dfs1(v);
	}
	f[++fcnt] = s;
}
 
void dfs2(int s)
{
	scc[s] = scnt;
	scc2[scnt]++;
	for(int i = head2[s] ;i != -1;i = e2[i].next)
	{
		int v = e2[i].to;
		if(!scc[v])
		    dfs2(v);	
	}	
}

void Kosaraju()
{
	dfs1(0);
	_for(i,1,n)
		if(!vis1[i])
			dfs1(i);

	_rep(i,0,fcnt)
	{
		
		int x = f[fcnt-i];
		if(!scc[x])
		{
			scnt++;
			dfs2(x);	
		}
	}	
	
} 

void dfs3(int x)
{
	vis2[x]=1;
	for(int i =head3[x];i!=-1;i=e3[i].next)
	{	
		int v = e3[i].to;
		if(!vis2[v])
		{		
		    ans[s] += scc2[v];
		    dfs3(v);
		} 		 
	}
}

void print()
{
	int flag = 0;
	_for(i,0,n)
	{			
	    if(ans2[scc[i]] == 1)
		{
			if(!flag)
			{
				cout << i;
				flag++;	
			}
			else cout << " " << i;
		} 
	}
	cout << endl;
	
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>T;
	
	_rep(i,1,T)
	{		
		int m;
		cin >> n >> m;
		inia();
		while(m--)
		{
			cin >> a >> b;
			addedge(a,b,1); 
			addedge(b,a,2);
		}
		cout<<"Case "<<i<<": ";
		Kosaraju();
		_for(i,0,n)
		{
			for(int j = head2[i]; j != -1;j = e2[j].next)
			{
				int v = e2[j].to; 
				if(scc[i] == scc[v])continue; 
				addedge(scc[i],scc[v],3);	
			}
   		}  
		   
		mx = 0; 
		_rep(i,1,scnt)
		{		
			if(!in_deg[i])
			{
				memset(vis2,0,sizeof(vis2));			
				ans[i] += scc2[i]-1;
			    s = i;
				dfs3(i);
				if(mx < ans[i])	mx = ans[i];			
			}		 
		}

		_rep(i,1,scnt)
		{
			if(ans[i] == mx) 
				ans2[i] = 1;
		}
	
		cout << mx << endl;
		print();
	}
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优秀委选举系统 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 优秀委选举 { public partial class Form1 : Form { private ListBox listBox; public Form1() { InitializeComponent(); } public Form1(ListBox listBox) { InitializeComponent(); this.listBox = listBox; } private void button1_Click(object sender, EventArgs e) { if (listBox1.Items.Count > 0) { if (listBox1.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { //获取左边listBox1的内容 string cont = listBox1.SelectedItem.ToString(); //左边listBox1删除选的内容 listBox1.Items.Remove(cont); //把获得的内容加入到右边的listBox2 listBox2.Items.Add(cont); } } } private void button2_Click(object sender, EventArgs e) { if (listBox2.Items.Count > 0) { if (listBox2.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { //获取右边listBox2的内容 string cont = listBox2.SelectedItem.ToString(); //右边listBox2删除选的内容 listBox2.Items.Remove(cont); //把获得的内容加入到左边的listBox1 listBox1.Items.Add(cont); } } } //退出 private void button5_Click(object sender, EventArgs e) { Application.Exit(); } //显示结果 private void button4_Click(object sender, EventArgs e) { //传递listBox2到Form2窗口 Form2 form2 = new Form2(listBox2); form2.Show(); } //修改名单 private void button3_Click(object sender, EventArgs e) { if (listBox2.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { string cont = listBox2.SelectedItem.ToString(); int index = listBox2.SelectedIndex; Form3 form3 = new Form3(cont, index, listBox2); form3.Show(); this.Hide(); } } //加载窗口 private void Form1_Load(object sender, EventArgs e) { if (listBox != null) { foreach (string str in listBox.Items) { listBox2.Items.Add(str); } } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值