题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6016
题目大意:中文题面http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=748&pid=1002
题目思路:总共三条边, 枚举中间一条边。
代码如下:
#include<bits/stdc++.h> using namespace std; vector<int>vecm[100007], vecg[100007]; int a[100007], b[100007]; void solve() { int n, m, k; scanf("%d%d%d", &n, &m, &k); int s = max(n, m); for(int i=1; i<=s; ++ i) { vecm[i].clear(); vecg[i].clear(); } for(int i=1; i<=k; ++ i) { scanf("%d %d", &a[i], &b[i]); vecm[a[i]].push_back(b[i]); vecg[b[i]].push_back(a[i]); } long long ans = 0; for(int i=1; i<=k; ++ i) { ans += (long long)(vecm[a[i]].size() - 1) * (long long)(vecg[b[i]].size() - 1); } printf("%I64d ", ans*2); } int main() { int t; scanf("%d", &t); while(t--) solve(); return 0; }