程序设计思维与实践 Week8 作业 (3/4/数据班)
程序设计思维与实践 Week8 作业 (3/4/数据班)
A - 区间选点 II
给定一个数轴上的 n 个区间,要求在数轴上选取最少的点使得第 i 个区间 [ai, bi] 里至少有 ci 个点
使用差分约束系统的解法解决这道题
Input
输入第一行一个整数 n 表示区间的个数,接下来的 n 行,每一行两个用空格隔开的整数 a,b 表示区间的左右端点。1 <= n <= 50000, 0 <= ai <= bi <= 50000 并且 1 <= ci <= bi - ai+1。
Output
输出一个整数表示最少选取的点的个数
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
问题分析
记 𝑠𝑢𝑚[𝑖] 表示数轴上 [0, 𝑖] 之间选点的个数,对于第 i 个区间 [𝑎𝑖, 𝑏𝑖] ,需要满足 𝑠𝑢𝑚[𝑏𝑖] − 𝑠𝑢𝑚 [𝑎𝑖 − 1] ≥ 𝑐𝑖,且满足0 ≤ 𝑠𝑢𝑚 [𝑖] − 𝑠𝑢𝑚[𝑖 − 1] ≤ 1 这个约束项。
转化为 ≥ 不等式组跑最长路,结果是𝑠𝑢𝑚[max{𝑏𝑖}]。
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
const int inf=0x3ffffff;
const int N=50005;
const int size=1000005;
struct Edge{
int to,next,w;
}e[size];
int head[N],tot;
void add(int v,int t,int w)
{
e[++tot].to=t,e[tot].w=w;
e[tot].next=head[v];
head[v]=tot;
}
int Max,vis[N],dis[N];
void spfa(int s)
{
queue<int>q;
for(int i=0;i<=Max;i++)
dis[i]=-inf,vis[i]=0;
q.push(s);
vis[s]=1;
dis[s]=0;
while(!q.empty())
{
int u=q.front();q.pop();
vis[u]=0;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(dis[v]<dis[u]+e[i].w)
{
dis[v]=dis[u]+e[i].w;
if(!vis[v])
{
q.push(v);
vis[v]=1;
}
}
}
}
}
int main() {
int n;
scanf("%d",&n);
fill(head,head+N,0);
tot=0;
for(int i=0;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v+1,w);
Max=max(Max,v+1);
}
for(int i=1;i<=Max;i++)
add(i-1,i,0),add(i,i-1,-1);
spfa(0);
printf("%d",dis[Max]);
return 0;
}
B - 猫猫向前冲
众所周知, TT 是一位重度爱猫人士,他有一只神奇的魔法猫。
有一天,TT 在 B 站上观看猫猫的比赛。一共有 N 只猫猫,编号依次为1,2,3,…,N进行比赛。比赛结束后,Up 主会为所有的猫猫从前到后依次排名并发放爱吃的小鱼干。不幸的是,此时 TT 的电子设备遭到了宇宙射线的降智打击,一下子都连不上网了,自然也看不到最后的颁奖典礼。
不幸中的万幸,TT 的魔法猫将每场比赛的结果都记录了下来,现在他想编程序确定字典序最小的名次序列,请你帮帮他。
Input
输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示猫猫的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即编号为 P1 的猫猫赢了编号为 P2 的猫猫。
Output
给出一个符合要求的排名。输出时猫猫的编号之间有空格,最后一名后面没有空格!
其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。
Sample Input
4 3
1 2
2 3
4 3
Sample Output
1 2 4 3
问题分析
胜负关系等价为一个有向图,a赢了b则在图中增加一条a到b的有向边。
如果两个猫的名次相同,则字典序小的那个排在前面。问题就可以转化为求字典序最小的拓扑序列。
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int MAXN=505;
const int MAXM=5000;
int N,M;
struct Edge{
int to,next;
}e[MAXM];
int head[MAXN],InDeg[MAXN],tot;
bool vis[MAXM];
struct cmp{
bool operator()(int r, int l){ return r>l; }
};
priority_queue<int,vector<int>,cmp > q;
vector<int> ans;
void init(){
tot=0;
fill(head,head+MAXN,-1);
fill(InDeg,InDeg+MAXN,0);
ans.clear();
}
void addEdge(int p1,int p2){
e[tot].to=p2;
e[tot].next=head[p1];
head[p1]=tot;
++InDeg[p2];
tot++;
}
void Kahn(){
for(int i=1;i<=N;++i){
if(!InDeg[i]) q.push(i);
}
memset(vis,true,sizeof(vis));
while(q.size()){
int p1=q.top(); q.pop();
ans.push_back(p1);
for(int i = head[p1]; i!=-1; i = e[i].next){
int p2 = e[i].to;
if(vis[i]){
if(--InDeg[p2]==0) q.push(p2);
vis[i]=false;
}
}
}
cout<<ans[0];
for(int i=1;i<N;++i) cout<<" "<<ans[i];
cout<<endl;
}
int main(){
int p1,p2;
while(~scanf("%d%d",&N,&M)) {
init();
for(int i=0;i<M;++i){
scanf("%d%d",&p1,&p2);
addEdge(p1,p2);
}
Kahn();
}
return 0;
}
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
问题分析
图中有环,必须将互相可达与单向可达分开考虑。两次dfs,第一次确定逆后序序列,第二次在反图(原图a->b的路径改记为b->a的路径)中按照逆后序序列进行遍历。
在边反向后的图中,对每个出度为 0 的点进行 dfs,计算其能到达的点的 SUM(SCC[ j]),即可得到答案。
在计算科学中,Kosaraju的算法(又称为–Sharir Kosaraju算法)是一个线性时间(linear time)算法找到的有向图的强连通分量。它利用了一个事实,逆图(与各边方向相同的图形反转, transpose graph)有相同的强连通分量的原始图。(资料来自网络)
#include <bits/stdc++.h>
using namespace std;
const int N=5005;
const int M=30005;
struct Edge{
int to,next;
};
Edge e1[M],e2[M],e3[M];
int head1[N],tot=0;
int head2[N];
int head3[N];
void add3(int u,int v)
{
e3[++tot].to=v;
e3[tot].next=head3[u];
head3[u]=tot;
}
void add(int u,int v)
{
e1[++tot].to=v;
e1[tot].next=head1[u];
head1[u]=tot;
e2[tot].to=u;
e2[tot].next=head2[v];
head2[v]=tot;
}
int n,scc,dfnt,c[N],dfn[N],vis[N];
void dfs1(int s)
{
vis[s]=1;
for(int i=head1[s];i!=-1;i=e1[i].next)
{
int u=e1[i].to;
if(!vis[u])dfs1(u);
}
dfn[++dfnt]=s;
}
void dfs2(int s)
{
c[s]=scc;
for(int i=head2[s];i!=-1;i=e2[i].next)
{
int u=e2[i].to;
if(!c[u])dfs2(u);
}
}
void kosaraju()
{
dfnt=scc=0;
for(int i=0;i<=n;i++)
vis[i]=0,c[i]=0;
for(int i=0;i<n;i++)
if(!vis[i])dfs1(i);
for(int i=n;i>=1;i--)
if(!c[dfn[i]])++scc,dfs2(dfn[i]);
}
int main() {
int T;
scanf("%d",&T);
for(int x=1;x<=T;x++)
{
int m;
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
head1[i]=-1,head2[i]=-1;
tot=0;
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
kosaraju();
set<int>ans;
int*sum=new int[scc+1];
int*cnt=new int[scc+1];
for(int i=0;i<=scc;i++)
sum[i]=0,cnt[i]=0,head3[i]=-1;
set<pair<int,int>> ee;
for(int k=0;k<n;k++)
{
sum[c[k]]++;
for(int i=head1[k];i!=-1;i=e1[i].next)
{
int u=e1[i].to;
if(c[u]!=c[k])
{
ee.insert(make_pair(c[u],c[k]));
}
}
}
tot=0;
for(auto it=ee.begin();it!=ee.end();it++)
{
add3(it->first,it->second);
cnt[it->second]++;
}
int Max=-1;queue<int>q;
for(int i=1;i<=scc;i++)
{
if(cnt[i]==0)q.push(i);
cnt[i]=0;
}
int*vvis=new int[scc+1];
while(!q.empty())
{
int temp=q.front();q.pop();
queue<int>qq;
qq.push(temp);
for(int i=1;i<=scc;i++)
vvis[i]=0;
vvis[temp]=1;
while(!qq.empty())
{
int now=qq.front();qq.pop();
cnt[temp]+=sum[now];
for(int i=head3[now];i!=-1;i=e3[i].next)
{
int ii=e3[i].to;
if(!vvis[ii])qq.push(ii),vvis[ii]=1;
}
}
Max=max(Max,cnt[temp]);
}
for(int i=1;i<=scc;i++)
if(cnt[i]==Max)ans.insert(i);
vector<int>last;
for(int i=0;i<n;i++)
{
if(ans.find(c[i])!=ans.end())
last.push_back(i);
}
printf("Case %d: %d\n",x,Max-1);
auto it=last.begin();
printf("%d",*it);it++;
while(it!=last.end())
printf(" %d",*it),it++;
printf("\n");
}
return 0;
}

浙公网安备 33010602011771号