UVaLive/LA 6804 Group of Strangers(图论)
2014-11-23 21:08
351 查看
![]() | 6804 - Group of Strangers |
// Author: Yuan Zhu #include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <cmath> #include <algorithm> #include <set> #define ll long long using namespace std; int t, n, m; vector<int> G[5010]; pair<int, int> p[20010]; int vis[5010][5010]; int marked; void init() { for(int i = 0; i < 5010; i++) G[i].clear(); } void read() { scanf("%d%d", &n, &m); int u, v; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); p[i] = make_pair(u, v); } } void solve(int ca) { marked++; ll S = n * 1LL * (n - 1) * 1LL * (n - 2) / 6; ll one = 0, two = 0, three = 0; int mm = 0; int marked1; int check[5010]; for (int i = 0; i < m; i++) { int u = p[i].first, v = p[i].second; if (vis[u][v] == marked) continue; vis[u][v] = marked; mm++; marked1++; for (int j = 0; j < G[u].size(); j++) check[G[u][j]] = marked1; ll ct = 0; for (int j = 0; j < G[v].size(); j++) { if (check[G[v][j]] == marked1) ct++; } /*set<int> s; for(int j=0;j<G[u].size();j++) s.insert(G[u][j]); for(int j=0;j<G[v].size();j++) s.insert(G[v][j]);*/ one += ((ll)n - (ll)G[u].size() - (ll)G[v].size() + ct); three += ct; //cout<<u<<" "<<v<<" "<<n-(ll)s.size()<<endl; } two = (mm * 1LL * (n - 2) - one - three) / 2; //cout<<one<<" "<<two<<" "<<three/3<<endl; ll ans = S - one - two - three / 3; printf("Case #%d: %lld\n", ca, ans); } int main() { scanf("%d", &t); for (int ca = 1; ca <= t; ca++) { init(); read(); solve(ca); } return 0; }
参考代码2:
// Author: Yejie Zhou #include <iostream> #include <cstdio> #include <vector> #include <cstring> #define ll long long using namespace std; int n, m; vector<int> g[5010]; bool vis[5010][5010]; int main () { int T; scanf("%d", &T); int ncase = 1; while (T--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { g[i].clear(); } memset(vis, 0, sizeof(vis)); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); vis[u][v] = 1; vis[v][u] = 1; } ll sum = 0; int three = 0; for (int i = 1; i <= n; i++) { int num = g[i].size(); if (num >= 2) sum += num * (num - 1) / 2; for (int j = 0; j < num - 1; j++) { for (int k = j + 1; k < num; k++) { if (vis[g[i][j]][g[i][k]]) three++; } } } three /= 3; ll ans = n * (n - 1) * (ll)(n - 2) / 6 - ((n - 2) * m - sum + three); printf("Case #%d: %lld\n", ncase++, ans); } return 0; }
相关文章推荐
- UVALive 6256 LA 6256 Who wants to live forever?
- LA 3266 || UVALive 3266 Tian Ji -- The Horse Racing 田忌赛马(贪心)
- ACM 图论 搜索 SPFA UVALive 5966 Blade and Sword
- UVALive/LA 5059 Play with Stones 组合游戏/SG定理
- UVaLive LA 4726 UVa 1451 - Average (子序列最大平均数 数形结合 斜率优化 单调队列)
- UVALive 4126 (LA 4126) Password Suspects AC自动机 + DP + 剪枝dfs
- Problem on Group Trip UVALive - 7219 (模拟+优先队列处理)
- UVALive 3713 浅谈2-SAT问题图论求解法
- UVALive(LA) 4487 Exclusive-OR(带权并查集)
- UVa Live (LA) 3644 典型并查集
- UVaLive LA 4356 - Fire-Control System (扫描法 思维)
- UVALive 4671 (LA 4671) K-neighbor substrings (2009年合肥) FFT
- UVaLive/LA 6800 The Mountain of Gold?(BellmanFord判负环+记忆化搜索)
- UVALive(LA) 4487 Exclusive-OR(带权并查集)
- UVALive 3942 (LA 3492) Remember the Word Trie树 + 记忆化搜索
- UVALive 3490 (LA 3940) || ZOJ 2619 Generator AC自动机(或KMP) + 整数高斯消元 + 数学期望
- UVaLive/LA 6800 The Mountain of Gold?(BellmanFord判负环+记忆化搜索)
- Let's Go Green UVALive - 6039题解 思维图论
- UVaLive/LA 6801 Sequence(DP)
- UVALive - 4256 || LA 4256 Salesmen 商人(DP)