-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finished AMONGUS2, working on laneswitch
- Loading branch information
Showing
16 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// https://www.codechef.com/INOIPRAC/problems/AMONGUS2 | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define MAX_N 500000 | ||
|
||
// "red" is true, "black" is false | ||
vector<bool> colors(MAX_N); | ||
vector<bool> vis(MAX_N, false); | ||
vector<vector<pair<int, int>>> adj(MAX_N); | ||
|
||
int numRed, numBlack; | ||
bool valid; | ||
|
||
void dfs(int v) | ||
{ | ||
vis[v] = true; | ||
|
||
if (colors[v]) | ||
++numRed; | ||
else | ||
++numBlack; | ||
|
||
for (int i = 0; i < adj[v].size(); ++i) | ||
{ | ||
const int &w = adj[v][i].first; | ||
const int &type = adj[v][i].second; | ||
|
||
bool newColor; | ||
if (type == 1) // v accuses w of being a parasite | ||
newColor = !colors[v]; | ||
else // v vouches that w is a human | ||
newColor = colors[v]; | ||
|
||
if (vis[w]) | ||
{ | ||
// validate (check if answer is -1) | ||
if (colors[w] != newColor) | ||
valid = false; | ||
} | ||
else | ||
{ | ||
colors[w] = newColor; | ||
dfs(w); | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int T; | ||
cin >> T; | ||
|
||
for (int t = 0; t < T; ++t) | ||
{ | ||
int N, Q; | ||
cin >> N >> Q; | ||
|
||
fill(vis.begin(), vis.end(), false); | ||
for (int i = 0; i < N; ++i) | ||
adj[i].clear(); | ||
|
||
int type, a, b; | ||
for (int i = 0; i < Q; ++i) | ||
{ | ||
cin >> type >> a >> b; | ||
--a, --b; | ||
adj[a].push_back({b, type}); | ||
adj[b].push_back({a, type}); | ||
} | ||
|
||
int ans = 0; | ||
for (int i = 0; i < N; ++i) | ||
if (!vis[i]) | ||
{ | ||
colors[i] = true; | ||
numRed = 0, numBlack = 0; | ||
valid = true; | ||
dfs(i); | ||
|
||
if (!valid) | ||
{ | ||
ans = -1; | ||
break; | ||
} | ||
|
||
ans += max(numRed, numBlack); | ||
} | ||
|
||
cout << ans << '\n'; | ||
} | ||
|
||
return 0; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// https://codeforces.com/problemset/problem/920/E | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define MAX_N 200000 | ||
|
||
vector<set<int>> reverseAdj(MAX_N); | ||
vector<int> numInComp(MAX_N, 0); | ||
set<int> unvisited; | ||
|
||
int currComp = 0; | ||
|
||
inline bool connected(int v, int w) | ||
{ | ||
// it's connected if it's not not connected | ||
return reverseAdj[v].find(w) == reverseAdj[v].end(); | ||
} | ||
|
||
void dfs(int v) | ||
{ | ||
unvisited.erase(v); | ||
//cout << v << " is in " << currComp << '\n'; | ||
++numInComp[currComp]; | ||
|
||
auto curr = unvisited.begin(); | ||
while (curr != unvisited.end()) | ||
{ | ||
int w = *curr; | ||
if (!connected(v, w)) | ||
{ | ||
++curr; | ||
continue; | ||
} | ||
|
||
dfs(w); | ||
curr = unvisited.upper_bound(w); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int n, m; | ||
cin >> n >> m; | ||
|
||
int a, b; | ||
for (int i = 0; i < m; ++i) | ||
{ | ||
cin >> a >> b; | ||
--a, --b; | ||
reverseAdj[a].insert(b); | ||
reverseAdj[b].insert(a); | ||
} | ||
|
||
for (int i = 0; i < n; ++i) | ||
unvisited.insert(i); | ||
|
||
while (!unvisited.empty()) | ||
{ | ||
dfs(*unvisited.begin()); | ||
++currComp; | ||
} | ||
|
||
sort(numInComp.begin(), numInComp.begin() + currComp); | ||
|
||
cout << currComp << '\n'; | ||
for (int i = 0; i < currComp; ++i) | ||
cout << numInComp[i] << ' '; | ||
cout << '\n'; | ||
|
||
return 0; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// https://open.kattis.com/contests/acpc17open/problems/laneswitching | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// TODO | ||
|
||
int main() | ||
{ | ||
int N, M, R; | ||
cin >> N >> M >> R; | ||
|
||
return 0; | ||
} |