洛谷P1726 上白澤慧音


題面如上


思路

跑他tarjan,然後以爆炸的時間複雜度暴力


代碼


#include <bits/stdc++.h>
#define maxn 100010
using namespace std ;
int n , m , tim , st[maxn] , top , si[maxn] , maxx ;
struct dy{
	int x , y , z , next ;
}a[maxn] ;
int head[maxn] , vis[maxn] , dfn[maxn] , low[maxn] , t , co[maxn] , col ;
vector<int>ans[maxn] ;int cnt ;
void add(int x ,int y ) {
	a[++t].x = x ;
	a[t].y = y ;
	a[t].z = 1 ;
	a[t].next = head[x] ;
	head[x] = t ;
}
void tarjan(int u) {
	dfn[u] = low[u] = ++tim ;
	st[++top] = u ;
	for(int i = head[u] ; i ;i  = a[i].next) {
		int v = a[i].y ;
		if(!dfn[v]) {
			tarjan(v) ;
			low[u] = min(low[u],low[v]) ;
		}else if(!co[v]) {
			low[u] = min(low[u],dfn[v]) ;
		}
	} 
	if(low[u] == dfn[u]) {
		co[u] = ++col ;
		++si[col] ;
		while(st[top] != u) {
			++si[col] ;
			co[st[top]] = col ;
			-- top ;
		}
		-- top ;
	}
}
int main () {
	cin  >> n >> m ;
	for(int i = 1 ; i <= m ; i ++) {
		int x , y , t ;
		cin >> x >> y >> t ;
		if(t == 1) {
			add(x,y) ;
		}else {
			add(x,y) ;
			add(y,x) ;
		}
	}
	for(int i = 1 ; i <= n ; i ++) {
		if(!dfn[i]) {
			tarjan(i) ;
		}
	}
	for(int i = 1 ; i <= col ; i ++) {
	//	cout << si[i] << " " ;
		maxx = max(maxx,si[i]) ;
	}
	cout << maxx << "\n" ;
	for(int i = 1 ; i <= col ; i ++) {
		if(si[i] == maxx) {
			cnt ++ ;
			for(int j = 1 ; j <= n ; j ++) {
				if(co[j] == i) {
					ans[cnt].push_back(j) ; 
				}
			}
			sort(ans[cnt].begin(),ans[cnt].end()) ;
		}
	}
	int lop = 0 , Max = 0 ;
	for(int i = 1 ; i <= cnt ; i ++) {
		if(ans[i][0] > Max) {
			lop = i ;
			Max = ans[i][0] ;
		}
	}
	for(int i = 0 ; i < ans[lop].size() ; i ++) {
		cout << ans[lop][i] << " " ;
	}
	return 0 ;
}

溜了溜了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章