UVaLive/LA 6804 Group of Strangers(图论)

FILE 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值