您的位置:首页 > 其它

UVaLive/LA 6804 Group of Strangers(图论)

2014-11-23 21:08 351 查看

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 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm-icpc 吉隆坡2014