UVaLive/LA 6804 Group of Strangers(图论)
来源:互联网 发布:西德尼谢尔顿 知乎 编辑:程序博客网 时间:2024/04/29 23:48
6804 - Group of Strangers
参考代码1:
// Author: Yuan Zhu#include <iostream>#include <cstdio>#include <vector>#include <cstring>#include <cmath>#include <algorithm>#include <set>#define ll long longusing 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;}
// Author: Yejie Zhou#include <iostream>#include <cstdio>#include <vector>#include <cstring>#define ll long longusing 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;}
0 0
- UVaLive/LA 6804 Group of Strangers(图论)
- UVaLive/LA 6800 The Mountain of Gold?(BellmanFord判负环+记忆化搜索)
- UVaLive/LA 6800 The Mountain of Gold?(BellmanFord判负环+记忆化搜索)
- UVaLive/LA 6801 Sequence(DP)
- UVaLive/LA 6802 Turtle Graphics(水题)
- UVaLive/LA 6805 Pantun Grader(模拟)
- LA 4329/uvalive 4329 (树状数组)
- UVaLive(LA) 3902 Network,Seoul 2007
- UVALive(LA) 3644 X-Plosives (并查集)
- UVaLive LA 4356 - Fire-Control System (扫描法 思维)
- UVaLive LA 4256 | UVa 1424 - Salesmen(简单DP)
- UVaLive ( LA ) 3516 - Exploring Pyramids (DP 递推)
- UVaLive/LA 6806 Hari Merdeka(AC自动机,DP)
- UVaLive/LA 6809 Spokes Wheel(搜索,二进制循环移位)
- UVALive - 4256 || LA 4256 Salesmen 商人(DP)
- UVaLive 2678(LA)Subsequence SEERC 2006 题解
- UVALive 6925Volume of Revolution(数学题)
- UVALive(LA) 4487 Exclusive-OR(带权并查集)
- SEO论坛
- wifi pineapple | WiFiPineapple plus 使用之将网页汉化为中文
- poj 1061 青蛙的约会
- UVA 10913 - Walking on a Grid
- SQLServer中的char、varchar、nchar、nvarchar
- UVaLive/LA 6804 Group of Strangers(图论)
- [LeetCode]Validate Binary Search Tree
- swift备忘-1:String和数值类型转换
- F#学习资源
- F#学习之路(1)什么是函数式编程
- F#学习之路(2) 深刻理解函数(下)
- 多线程生产者和消费者例子
- opencv图像遍历方法速度对比
- F# 学习之路(3) 如何组织程序(上)