把割顶都去掉了吗,最后乘法原理什么的,搞一搞就过了,交了那么多次,居然是因为最后输出没用 long long,浪费我的提交 次数,加一个优化,如果一个连通分量已经dfs到它连着超过1个更定了,就结束dfs
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; #define LL long long LL head[100100], next[100100]; LL u[100100], v[100100]; LL pre[100010]; bool iscut[100010]; LL cnt1; bool vis[100010]; LL dfs_clock; LL block[100010]; LL cnt2; LL kk[100010], tot; bool vis_block[100010]; LL dfs(LL x, LL fa){ LL lowx = pre[x] = ++ dfs_clock; LL cnt = 0; for(LL i = head[x]; i != -1; i = next[i]){ LL y = v[i]; if(!pre[y]){ cnt ++; LL lowy = dfs(y, x); lowx = min(lowx, lowy); if(lowy >= pre[x]) iscut[x] = true; } else if(pre[y] < pre[x] && y != fa){ lowx = min(lowx, pre[y]); } } if(cnt == 1 && fa < 0) iscut[x] = false; return lowx; } void solve(LL x, LL fa){ vis[x] = 1; block[x] = cnt1; for(LL i = head[x]; i != -1; i = next[i]){ LL y = v[i]; if(!vis[y]){ solve(y, x); } } } void get_ans(LL x, LL fa){ vis[x] = 1; cnt2 ++; if(cnt1 > 1) return; for(LL i = head[x]; i != -1; i = next[i]){ LL y = v[i]; if(cnt1 > 1) return; if(!vis[y]){ if(iscut[y]){ vis[y] = true; kk[++ tot] = y; cnt1 ++; continue; } get_ans(y, x); } } } int main(){ LL n; LL h = 0; while(scanf("%lld", &n) != EOF){ if(n == 0) return 0; h ++; memset(head, -1, sizeof(head)); LL m = 0; for(LL i = 1; i <= n; i ++){ scanf("%lld%lld", &u[2 * i - 1], &v[2 * i - 1]); m = max(m, u[2 * i - 1]); m = max(m, v[2 * i - 1]); u[2 * i] = v[2 * i - 1]; v[2 * i] = u[2 * i - 1]; next[2 * i - 1] = head[u[2 * i - 1]]; head[u[2 * i - 1]] = 2 * i - 1; next[2 * i] = head[u[2 * i]]; head[u[2 * i]] = 2 * i; } dfs_clock = 0; memset(iscut, 0, sizeof(iscut)); memset(pre, 0, sizeof(pre)); LL wl = dfs(1, -1); for(LL i = 1; i <= m; i ++) vis[i] = iscut[i]; for(LL i = 1; i <= m; i ++){ if(!vis[i]){ cnt1 ++; solve(i, -1); } } memset(vis, 0, sizeof(vis)); LL ans1 = 0, ans2 = 1; tot = 0; memset(vis_block, 0, sizeof(vis_block)); for(LL i = 1; i <= m; i ++){ if(!iscut[i] && !vis[i] && !vis_block[block[i]]){ cnt1 = 0; cnt2 = 0; get_ans(i, -1); if(cnt1 == 1){ ans1 ++; ans2 *= cnt2; } for(LL j = 1; j <= tot; j ++){ vis[kk[j]] = 0; } tot = 0; vis_block[block[i]] = 1; } } if(ans1 == 0) printf("Case %lld: 2 %lld\n", h, m * (m - 1) / 2); else printf("Case %lld: %lld %lld\n", h, ans1, ans2); } return 0; }