1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <iostream> #include <vector> #include <cstring> #include <algorithm>
using namespace std; const int maxn = 1e5+10; int n,m;
int head[maxn],next1[maxn],ver[maxn]; int dfn[maxn],low[maxn],stack_[maxn],in_stack[maxn]; int in[maxn],out[maxn],belong[maxn]; int scount[maxn]; int tot,cnt,num,top;
void add(int x,int y) { ver[++tot] = y; next1[tot] = head[x]; head[x] = tot; }
void tarjan(int x) { dfn[x] = low[x] = ++num; stack_[++top] = x; in_stack[x] = 1; for (int i=head[x]; i; i=next1[i]) { int to = ver[i]; if (!dfn[to]) { tarjan(to); low[x] = min(low[x],low[to]); } else if (in_stack[to]) low[x] = min(low[x],dfn[to]); } if (dfn[x] == low[x]) { cnt++; int y; do{ y = stack_[top--]; belong[y] = cnt; in_stack[y] = 0; scount[cnt]++; } while(y != x); } }
void init() { tot = cnt = num= top = 0; memset(head,0,sizeof head); memset(ver,0,sizeof ver); memset(next1,0,sizeof next1); memset(in_stack,0,sizeof in_stack); memset(stack_,0,sizeof stack_); memset(dfn,0,sizeof dfn); memset(low,0,sizeof low); memset(in,0,sizeof in); memset(out,0,sizeof out); memset(belong,0,sizeof belong); memset(scount,0,sizeof scount); }
int main() { freopen("1.in","r",stdin); int times; cin >> times; for (int _times=1; _times<=times; ++_times) { init(); scanf("%d%d",&n,&m); for (int i=1; i<=m; ++i) { int x,y; scanf("%d%d",&x,&y); add(x,y); } for (int i=1; i<=n; ++i) if(!dfn[i]) tarjan(i); if (cnt == 1) { printf("Case %d: -1\n",_times); continue; } for (int i=1; i<=n; ++i) for (int j=head[i]; j;j=next1[j]) { int to = ver[j]; if (belong[i] != belong[to]) { in[belong[to]]++; out[belong[i]]++; } } int x = 0x3f3f3f3f; for (int i=1; i<=cnt; ++i) { if (!in[i]) { x = min(x,scount[i]); } if (!out[i]) { x = min(x,scount[i]); } } printf("Case %d: %d\n",_times,n*n-n-x*(n-x)-m); } return 0; }
|