WEEK8-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

思路
有向图一般的思路就是先用Kahn算法求出强连通分量SCC,然后缩点,也就是将互相可到达和单向可到达分开。
缩点后,不难发现对于属于第i个SCC的点来说,答案分为
两部分,令SCC[i]表示第i个SCC中点的个数
当前SCC中的点,ans+=SCC[i]-1(去除自己)
其它SCC中的点
SUM(SCC[j]),其中j可到达I
稍加思考,可以发现最后答案一定出现在出度为0的SCC
中,
可以用反证法证明
因此我们将边反向,对每个入度为0的点进行dfs,计算其
能到达的点的SUM(SCC[j]),即可得到答案。
Kosaraju算法的步骤:
算法步骤
第一-遍dfs确定原图的逆后序序列
第二遍dfs在反图中按照逆后序序列进行遍历
反图即将原图中的有向边反向
每次由起点遍历到的点即构成一个SCC
(现在做题越来越吃力了,感觉自己好菜,刚开始的算法还能一边理解一边证明一边记忆,现在算法就是直接按照步骤和ppt上代码来,证明什么的根本不敢想)

代码

#include<bits/stdc++.h>
#define mn 5010
#define mm 30010
using namespace std;
int N,M;
int tot1=0,tot2=0,tot3=0,dcnt,scnt,ans,num=1;
int head1[mn],head2[mn],head3[mn],in_deg[mn],dfn[mn],scc[mn],aans[mn],vis1[mn],vis2[mn],vis3[mn],mscc[mn];
struct edge{
   
 int v,next;
};
void init(){
   
 tot1=0,tot2=0,tot3=0;
 dcnt=0,scnt=0,ans=0;
 for(int i=0;i<mn;i++){
   
  head1[i]=-1;
  head2[i]=-1;
  head3[i]=
优秀班委选举系统 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、付费专栏及课程。

余额充值