Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[유니온 파인드]11월 29일 #18

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions [union find]1129/1043.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// Created by LG on 2021-11-27.
//
#include <iostream>
#include <vector>

using namespace std;

vector<int> parent;
vector<bool> truth;

int findParent(int node){
if(parent[node] < 0)
return node;
return parent[node] = findParent(parent[node]);
}

void unionInput(int x, int y){
int xp = findParent(x);
int yp = findParent(y);

if(xp == yp)
return;
if(truth[xp] || truth[yp])
truth[xp] = truth[yp] = true;
if(parent[xp] < parent[yp]){
parent[xp] += parent[yp];
parent[yp] = xp;
}
else{
parent[yp] += parent[xp];
parent[xp] = yp;
}
}

int main(){
int n, m, k;
cin >> n >> m >> k;
int cnt = m;
parent.assign(n+1, -1);
truth.assign(n+1, false);
vector<vector<int>> party(m, vector<int>());

for(int i=0; i<k; i++){
int p;
cin >> p;
truth[p] = true;
}

for(int i=0; i<m; i++){
int t;
cin >> t;
party[i].assign(t, 0);

for(int j=0; j<t; j++){
int p;
cin >> p;
party[i][j] = p;
}

for(int j=0; j<t; j++){
for(int l=j+1; l<t; l++)
unionInput(party[i][j], party[i][l]);
}
}

for(int i=0; i<m; i++){
for(int j=0; j<party[i].size(); j++){
if(truth[findParent(party[i][j])]){
cnt--;
break;
}
}
}

cout << cnt;
return 0;
}
58 changes: 58 additions & 0 deletions [union find]1129/18111.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Created by LG on 2021-11-28.
//
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

const int MAX = INT_MAX;

int main(){
int n, m, b;
cin >> n >> m >> b;

vector<vector<int>> height(n, vector<int>(m, 0));
int min_h = 256;
int max_h = 0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++) {
cin >> height[i][j];
if(height[i][j] < min_h)
min_h = height[i][j];
if(height[i][j] > max_h)
max_h = height[i][j];
}
}

int ans_time = MAX;
int ans_h;
while(min_h <= max_h){
int time = 0;
int b_temp = b;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(height[i][j] > min_h){
int diff = height[i][j] - min_h;
time += 2*diff;
b_temp += diff;
}
else if(height[i][j] < min_h){
int diff = min_h - height[i][j];
time += diff;
b_temp -= diff;
}
}
}

if(b_temp >= 0 && time <= ans_time) {
ans_time = time;
ans_h = min_h;
}
min_h++;
}

cout << ans_time << ' ' << ans_h;
return 0;
}
74 changes: 74 additions & 0 deletions [union find]1129/1976.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Created by LG on 2021-11-28.
//
#include <iostream>
#include <vector>

using namespace std;

vector<int> parent;

int findParent(int node){
if(parent[node] < 0)
return node;
else
return parent[node] = findParent(parent[node]);
}

void unionInput(int x, int y){
int xp = findParent(x);
int yp = findParent(y);

if(xp == yp)
return;
if(parent[xp] < parent[yp]){
parent[xp] += parent[yp];
parent[yp] = xp;
}
else{
parent[yp] += parent[xp];
parent[xp] = yp;
}

}

bool result(int m, vector<int> route){
for(int i=0; i<m-1; i++){
int p1 = findParent(route[i]);
int p2 = findParent(route[i+1]);
if(p1 != p2)
return false;
}
return true;
}

int main(){
int n, m;
cin >> n >> m;
parent.assign(n+1, -1);
vector<vector<int>> cities(n+1, vector<int>(n+1));
vector<int> route(m, 0);
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cin >> cities[i][j];
}
}

for(int i=0; i<m; i++){
cin >> route[i];
}

for(int i=1; i<=n; i++){
for(int j=i+1; j<=n; j++){
if(cities[i][j]==1)
unionInput(i, j);
}
}

if(result(m, route))
cout << "YES";
else
cout << "NO";

return 0;
}
48 changes: 48 additions & 0 deletions [union find]1129/20040.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Created by LG on 2021-11-27.
//
#include <iostream>
#include <vector>

using namespace std;

vector<int> parent;

int findParent(int node){
if(parent[node] < 0)
return node;
return parent[node] = findParent(parent[node]);
}

bool unionInput(int x, int y){
int xp = findParent(x);
int yp = findParent(y);

if(xp == yp){
return true;
}
if(parent[xp] < parent[yp]){
parent[xp] += parent[yp];
parent[yp] = xp;
}
else{
parent[yp] += parent[xp];
parent[xp] = yp;
}
return false;
}

int main(){
int n, m, a, b;
cin >> n >> m;
parent.assign(n, -1);
for(int i=0; i<m; i++){
cin >> a >> b;
if(unionInput(a, b)) {
cout << i+1;
return 0;
}
}
cout << '0';
return 0;
}