Subtask #4 TLE求调(蚂蜂优良)
查看原帖
Subtask #4 TLE求调(蚂蜂优良)
809765
Limitless_lmw楼主2024/11/29 22:42

RT

大体上是按照深进的风格写的。

#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_map>
using namespace std;

const int maxn=2e6+5;
int head[maxn], nxt[maxn], to[maxn], tot = 1;

inline void add_V(int b,int e){
	nxt[++tot] = head[b];
	to[head[b] = tot] = e;
	return ;
}

int dfn[maxn], low[maxn], st[maxn], top;
bool ins[maxn];
int cnt, idx, bel[maxn];
vector<int> ans[maxn];

struct HashFunc
{
    template<typename T, typename U>
    size_t operator()(const std::pair<T, U>& p) const {
    return std::hash<T>()(p.first) ^ std::hash<U>()(p.second);
    }
};

struct EqualKey {
    template<typename T, typename U>
    bool operator ()(const std::pair<T, U>& p1, const std::pair<T, U>& p2) const {
    return p1.first == p2.first && p1.second == p2.second;
    }
};

unordered_map<pair<int,int>, int, HashFunc, EqualKey> mmap;
int anscnt;
int n,m;

void dfs(int u, int lst){
	low[u] = dfn[u] = ++cnt;
	st[++top] = u;
	for(int i = head[u]; i; i = nxt[i]){
		if(i == (lst ^ 1)) continue;
		if(!dfn[to[i]]){
			dfs(to[i], i);
			low[u] = min(low[u],low[to[i]]);
		}
        else low[u] = min(low[u],dfn[to[i]]);
	}
	if(low[u] == dfn[u]){
		int v; ++idx;
		do{
			v = st[top--];
			bel[v] = idx;
			ans[bel[v]].push_back(v);
		}while(u ^ v);
	}
	return ;
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i = 1,u,v; i<=m; i++){
		scanf("%d %d",&u,&v);
        if(mmap[make_pair(u,v)]>=2 || mmap[make_pair(v,u)]>=2) continue;
		add_V(u,v),add_V(v,u);
        mmap[make_pair(u,v)]++, mmap[make_pair(v,u)]++;
	}
	for(int i = 1; i<=n; i++){
		if(!dfn[i]) dfs(i,-1);
	}
	printf("%d\n",idx);
	for(int i = 1; i<=idx; i++){
		printf("%lu ",ans[i].size());
		for(auto t:ans[i]) printf("%d ",t);
		puts("");
	}
	return 0;
}
2024/11/29 22:42
加载中...