/**************************************************************
Problem:hdu 5695
User: youmi
Language: C++
Result: Accepted
Time:655 MS
Memory:3632 K
****************************************************************/
//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <cmath>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#define zeros(a) memset(a,0,sizeof(a))
#define ones(a) memset(a,-1,sizeof(a))
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scs(a) scanf("%s",a)
#define sclld(a) scanf("%I64d",&a)
#define pt(a) printf("%d\n",a)
#define ptlld(a) printf("%I64d\n",a)
#define rep0(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define rep_1(i,n) for(int i=n;i>=1;i--)
#define rep_0(i,n) for(int i=n-1;i>=0;i--)
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define lson (step<<1)
#define rson (lson+1)
#define esp 1e-6
#define oo 0x3fffffff
#define TEST cout<<"*************************"<<endl

using namespace std;
typedef long long ll;
int n,m;
const int maxn=100000+10;
struct side
{
int v,next;
}e[maxn];
int in[maxn];
priority_queue<int>q;
void init()
{
T=0;
zeros(in);
q.empty();
}

void build(int u,int v)
{
e[T].v=v;
}
void dfs(int u)
{
{
int v=e[i].v;
in[v]--;
if(in[v]==0)
q.push(v);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T_T;
scanf("%d",&T_T);
for(int kase=1;kase<=T_T;kase++)
{
//printf("Case #%d:\n",kase);
sc2(n,m);
init();
for(int i=1;i<=m;i++)
{
int a,b;
sc2(a,b);
build(a,b);
in[b]++;
}
for(int i=1;i<=n;i++)
if(in[i]==0)
q.push(i);
ll ans=0;
int mn=n+10;
while(!q.empty())
{
int temp=q.top();
mn=Min(temp,mn);
q.pop();
ans+=mn;
dfs(temp);
}
ptlld(ans);
}
return 0;
}

posted on 2016-05-27 15:19 中子星 阅读(...) 评论(...) 编辑 收藏