标签:
题目链接:
Time Limit: 6000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
//#include <bits/stdc++.h> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; //const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=0x3f3f3f3f; const int N=1e5+25; int n,m,ind[N],vis[N],b[N]; vector<int>ve[N]; priority_queue<int>qu; int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); Riep(n)ve[i].clear(),vis[i]=0; int x,y; Riep(m) { scanf("%d%d",&x,&y); ve[x].push_back(y); ind[y]++; } for(int i=1;i<=n;i++) { if(!ind[i]) { qu.push(i); vis[i]=1; } } int cnt=0; while(!qu.empty()) { int fr=qu.top(); b[cnt++]=fr; qu.pop(); int len=ve[fr].size(); for(int i=0;i<len;i++) { int y=ve[fr][i]; ind[y]--; if(!ind[y]&&!vis[y]) { qu.push(y); vis[y]=1; } } } LL sum=0; int mmin=2e5; for(int i=0;i<n;i++) { if(mmin>b[i]) { mmin=b[i]; } sum=sum+mmin; } printf("%I64d\n",sum); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5515273.html