알고리즘 공부

백준 알고리즘 수업- 깊이 우선탐색 2

컴퓨터과학 2023. 7. 30. 15:39

https://www.acmicpc.net/problem/24480

 

24480번: 알고리즘 수업 - 깊이 우선 탐색 2

첫째 줄에 정점의 수 N (5 ≤ N ≤ 100,000), 간선의 수 M (1 ≤ M ≤ 200,000), 시작 정점 R (1 ≤ R ≤ N)이 주어진다. 다음 M개 줄에 간선 정보 u v가 주어지며 정점 u와 정점 v의 가중치 1인 양

www.acmicpc.net

이거 참 ㅋㅋ 문제가 시간초과 나길래 계속 for문을 줄여도 안되서 

확인해보니 endl->\n으로 출력을바꾸니 해결됬네요.. ㅎ 

그냥 차라리 시간복잡도 관련된거면 이해가 가는데 이런걸로 시간초과는 조금 아니네요 ㅎ 

#include <iostream>
#include <vector>
#include <queue>
#include<string>
#include<algorithm>
#include<cmath>
#include<unordered_map>
#include<map>
#include<stack>
using namespace std;

struct Point {
	int s, e, t, cnt, power;
	char c;
};

void Prints(vector<vector<int>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void Prints(vector<vector<bool>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j];
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<vector<char>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j];
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<vector<vector<char>>>maps, int n, int m, int k) {
	for (int t = 0; t < k; t++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cout << maps[t][i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
	cout << endl;
}

void Prints(vector<vector<vector<bool>>>maps, int n, int m, int k) {
	for (int t = 0; t < k; t++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cout << maps[t][i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(string str, int n) {
	for (int i = 0; i < n; i++) {
		cout << str[i] << ",";
	}
	cout << endl;
}

struct Data {
	unsigned long long index;
	unsigned long long value;
};

bool compares(const Point &a, const Point &b) {
	if (a.s == b.s) {
		return a.e > b.e;
	}
	return a.s > b.s;
}
vector<bool>bVisited;
vector<Point>nodes;
map<int, int>anw;
vector<vector<int>>anws;
int n, m, r;
int gCnt = 0;
vector<vector<int>>glists;
void dfs(int r) {
	bVisited[r-1] = true;
	//4 
	//int start = lists[r][0]; //4
	//int end = lists[r][lists[r].size() - 1];//2
	//4,2
	for (int i = 0; i < glists[r].size();i++) {
		
		
		if (bVisited[glists[r][i]-1] == false) {
			//cout << glists[r][i] << endl;
			gCnt++;
			anw[glists[r][i]] = gCnt;
			//4
			dfs(glists[r][i]);
		}
		
	}
	return;
}
int main() {
	
	cin >> n >> m >> r;
	vector<vector<int>>lists(n+1);
	
	for (int i = 0; i < n; i++) {
		bVisited.push_back(false);
		anw[i + 1] = 0;
		
	}
	
	for (int i = 0; i < m; i++) {
		int s, e;
		cin >> s;
		cin >> e;
		Point p;
		p.s = s;
		p.e = e;
		nodes.push_back(p);	
	}
	sort(nodes.begin(), nodes.end(), compares);

	
	for (int i = 0; i < m; i++) {
		lists[nodes[i].s].push_back(nodes[i].e);
		lists[nodes[i].e].push_back(nodes[i].s);
	}
	glists = lists;
	gCnt++;
	anw[r] = gCnt;
	dfs(r);
	for (auto iter = anw.begin(); iter != anw.end(); iter++) {
		cout << iter->second<<'\n';
	}

	return 0;
}