QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#542385#7860. Graph of Maximum Degree 3RepeaterTL 0ms3620kbC++202.2kb2024-09-01 01:00:182024-09-01 01:00:19

Judging History

你现在查看的是最新测评结果

  • [2024-09-01 01:00:19]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3620kb
  • [2024-09-01 01:00:18]
  • 提交

answer

#include <bits/stdc++.h>

struct DSU 
{
    std::vector<int> f, siz;
    
    DSU() {}
    DSU(int n) 
    {
        init(n);
    }
    
    void init(int n) 
    {
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        siz.assign(n, 1);
    }
    
    int find(int x) 
    {
        while (x != f[x]) 
        {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    
    bool same(int x, int y) 
    {
        return find(x) == find(y);
    }
    
    bool merge(int x, int y) 
    {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        siz[x] += siz[y];
        f[y] = x;
        return true;
    }
    
    int size(int x) 
    {
        return siz[find(x)];
    }
};

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int n, m; std::cin >> n >> m;
	DSU dsu(n);

	std::vector<std::vector<int>> adj(n);
	std::map<std::pair<int, int>, int> vis;
	for(int i = 0; i < m; i++)
	{
		int u, v, w; std::cin >> u >> v >> w;
		u--, v--;

		if(w == 0)
		{
			adj[u].emplace_back(v);
			adj[v].emplace_back(u);
		}
		else
		{
			vis[{u, v}] = 1;
			vis[{v, u}] = 1;
		}
	}

	std::vector<int> fa(n);
	auto init = [&](auto init, int x, int lst) -> void
	{
		fa[x] = lst;
		for(auto y : adj[x])
		{
			if(y == lst) continue;
			init(init, y, x);
		}
	};
	init(init, 0, -1);

	std::vector<int> tmp;

	auto check = [&]() -> int
	{
		int ok = 1;
		for(int i = 0; i < tmp.size(); i++)
			for(int j = i + 1; j < tmp.size(); j++)
				if(vis.contains({tmp[i], tmp[j]}) || vis.contains({tmp[j], tmp[i]}))
					dsu.merge(tmp[i], tmp[j]);

		for(int i = 1; i < tmp.size(); i++)
			if(!dsu.same(tmp[0], tmp[1])) ok = 0;

		for(auto i : tmp) dsu.f[i] = i;

		return ok;
	};	

	int ans = 0;
	auto cacl = [&](auto cacl, int x) -> void
	{
		tmp.emplace_back(x);
		ans += check();

		if(tmp.size() == 4) return;
		for(auto y : adj[x])
		{
			if(y == fa[x]) continue;
			cacl(cacl, y);
		}
		tmp.pop_back();
	};

	for(int i = 0; i < n; i++) cacl(cacl, i);

	std::cout << ans << "\n";

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3620kb

input:

3 4
1 2 0
1 3 1
2 3 0
2 3 1

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

4 6
1 2 0
2 3 0
3 4 0
1 4 1
2 4 1
1 3 1

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: -100
Time Limit Exceeded

input:

20 28
9 6 1
9 6 0
3 8 0
8 4 0
3 8 1
3 4 1
2 13 0
13 1 0
19 1 0
2 1 1
2 19 1
13 19 1
14 15 1
14 15 0
7 12 0
12 17 0
20 17 0
7 17 1
7 20 1
12 20 1
16 18 0
18 10 0
5 10 0
16 10 1
16 5 1
18 5 1
4 6 0
9 11 0

output:


result: