记录编号 398358 评测结果 AAAAA
题目名称 通讯问题 最终得分 100
用户昵称 GravatarHeHe 是否通过 通过
代码语言 C++ 运行时间 0.002 s
提交时间 2017-04-22 08:05:14 内存使用 0.36 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 110;

template<typename T>
class heap{
private:
	T s[MAXN];
	int en;
public:
	heap(){
		en = 0;
	}
	void push(const T& a){
		int now;
		s[now = ++en] = a;
		while(now ^ 1 && a < s[now >> 1]){
			s[now] = s[now >> 1];
			now >>= 1;
		}
		s[now] = a;
		return ;
	}
	
	T top()const{
		return s[1];
	}
	
	void pop(){
		s[1] = s[en];
		--en;
		T p = s[1];
		int now = 1, tmp1, tmp2;
		while((tmp1 = now << 1) <= en && p > min(s[tmp1], s[tmp2 = tmp1 | 1])){
			if(s[tmp1] < s[tmp2]){
				s[now] = s[tmp1];
				now = tmp1;
			}
			else{
				s[now] = s[tmp2];
				now = tmp2;
			}
		}
		s[now] = p;
		return ;
	}
	
	int size()const{
		return en;
	}
};

struct EDGE{
	int to, ne;
	
	EDGE(){;}
	EDGE(int a, int b){
		to = a, ne = b;
	}
};

inline void add_edge(int fr, int to);
void dfs(int u);
bool cmp(const heap<int>& a, const heap<int>& b);

vector<EDGE> edge;
heap<int> p[MAXN];
int head[MAXN];
int N, a, b, ans_cnt;
int dfn[MAXN], low[MAXN];
int stack[MAXN], top;
bool ins[MAXN];

int main(){
#ifndef LOCAL
	freopen("jdltt.in", "r", stdin);
	freopen("jdltt.out", "w", stdout);
#else
	freopen("test.in", "r", stdin);
#endif
	memset(head, 0xff, sizeof(head));
	
	scanf("%d", &N);
	while(scanf("%d %d", &a, &b) != EOF)add_edge(a, b);
	
	for(int i = 1; i <= N; ++i)if(!dfn[i])dfs(i);
	
	printf("%d\n", ans_cnt);
	
	sort(p, p + ans_cnt, cmp);
	
	for(int i = 0; i < ans_cnt; ++i){
		while(p[i].size()){
			printf("%d ", p[i].top());
			p[i].pop();
		}
		putchar('\n');
	}
	
	return 0;
}

inline void add_edge(int fr, int to){
	edge.push_back(EDGE(to, head[fr])), head[fr] = edge.size() - 1;
	return ;
}

void dfs(int u){
	static int cnt = 0;
	int v;
	dfn[u] = low[u] = ++cnt;
	stack[top++] = u;
	ins[u] = true;
	
	for(int i = head[u]; ~i; i = edge[i].ne){
		if(!dfn[v = edge[i].to]){
			dfs(v);
			low[u] = min(low[u], low[v]);
		}
		else if(ins[v]){
			low[u] = min(low[u], dfn[v]);
		}
	}
	
	if(low[u] == dfn[u]){
		int tmp;
		do{
			--top;
			p[ans_cnt].push(tmp = stack[top]);
			ins[tmp] = false;
			// printf("%d ", tmp);
		}while(u != tmp);
		++ans_cnt;
		// putchar('\n');
	}
	
	return ;
}

bool cmp(const heap<int>& a, const heap<int>& b){
	return a.top() < b.top();
}