首页 > 其他分享 >HDU 5638 Toposort

HDU 5638 Toposort

时间:2022-11-09 18:32:02浏览次数:42  
标签:Toposort HDU 5638 int cnt number -- include mod


Problem Description


n vertices and  m edges. You are allowed to delete exact  k


 



Input


T indicating the number of test cases. For each test case:

The first line contains three integers  n,  m and  k  (1≤n≤100000,0≤k≤m≤200000) -- the number of vertices, the number of edges and the number of edges to delete.

For the next  m lines, each line contains two integers  ui and  vi, which means there is a directed edge from  ui to  vi  (1≤ui,vi≤n).

You can assume the graph is always a dag. The sum of values of  n in all test cases doesn't exceed  106. The sum of values of  m in all test cases doesn't exceed  2×106.


 



Output


S=(∑i=1ni⋅pi) mod (109+7), where  p1,p2,...,pn


 



Sample Input


3 4 2 0 1 2 1 3 4 5 1 2 1 3 1 4 1 2 3 2 4 4 4 2 1 2 2 3 3 4 1 4


 



Sample Output


30 27 30


 


有向图删除k条边使得拓扑序列字典序最小,可以直接用优先队列维护。


#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<functional>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
const int mod=1e9+7;
int T,n,m,k,cnt[maxn],x,y,vis[maxn];
vector<int> t[maxn];

int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d%d",&n,&m,&k);
for (int i=1;i<=n;i++) t[i].clear(),cnt[i]=vis[i]=0;
while (m--)
{
scanf("%d%d",&x,&y);
t[x].push_back(y);
cnt[y]++;
}
priority_queue<int,vector<int>,greater<int> > p;
for (int i=1;i<=n;i++) if (cnt[i]<=k) p.push(i),vis[i]=1;
int res=0,i=1;
while (!p.empty())
{
int q=p.top(); p.pop();
if (cnt[q]>k) {vis[q]=0; continue;}
(res+=(LL)q*i++%mod)%=mod;
k-=cnt[q];
for (int i=0;i<t[q].size();i++)
{
cnt[t[q][i]]--;
if (!vis[t[q][i]]&&cnt[t[q][i]]<=k)
{
p.push(t[q][i]);
vis[t[q][i]]=1;
}
}
}
printf("%d\n",res);
}
return 0;
}



标签:Toposort,HDU,5638,int,cnt,number,--,include,mod
From: https://blog.51cto.com/u_15870896/5837736

相关文章

  • HDU 4651 Partition
    ProblemDescriptionHowmanywayscanthenumbers1to15beaddedtogethertomake15?Thetechnicaltermforwhatyouareaskingisthe"numberofpart......
  • HDU 1028
    如果只有\(1\)数字,多项式为:\(1+x+x^2+x^3+\ldots\)。如果只有\(2\)数字,多项式为:\(1+x^2+x^4+x^6+\ldots\)。……如果只有\(k\)数字,多项式为:\(1+x^k+x^{2k}+x^{3......
  • B - K-th Number HDU - 6231 (二分 尺取)
    WindowsSource2017中国大学生程序设计竞赛-哈尔滨站题意给一个数组,在所有长度大于等于k的区间内,找出第\(k\)大的数,放到另一个数组中,然后在新数组中找到第M大的数。思......
  • HDU-1260 Tickets
    感觉题目还是比较水的,我这个蒟蒻也能写出来hh。思路:f[i]是前i个人(包含第i个)买票需要花费的总时间,第i个人买票所需时间,可以自己单买(f[i-1]+a[i]),也可以和前面那个人拼......
  • HDU 1686Oulipo ———————Hash or KMP
    OulipoOulipoTimeLimit:3000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):22302    AcceptedSubmission(s):86......
  • HDU 2050折线分割平面(递推)
    折线分割平面TimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):36479    AcceptedSubmission(s):244......
  • HDU2535Vote
     TimeLimit:5000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):7302    AcceptedSubmission(s):3735 Probl......
  • [题解] HDU7060 Separated Number 思路整理
    题目链接HDU7060SeparatedNumber题目大意给一个\(n\)位数,把该数字分成\(k\)段,每种方案的贡献为其分割出的段的数字之和。求所有分法的贡献之和(对\(998244353\)......
  • HDU - 2717
    大概是自己第一次在图之外用搜索吧(wwww要是早点做过的话可能蓝桥省赛的那个记忆化搜索就能a出来了hhhhttps://vjudge.net/problem/HDU-2717#author=shiyifan(这是hdu源链......
  • 树上连通有关背包:【BZOJ4182】shopping &【HDU6566】The Hanged Man
    选这两道题是因为这两道题都是树上背包,而且选的点的要求都与连通性有关,而且都是按dfs序DP来模拟不断加入物品,而且都能用树剖和点分治优化(不过优化的点一个跟子树大小有......