Skip to content

Commit

Permalink
finished AMONGUS2, working on laneswitch
Browse files Browse the repository at this point in the history
  • Loading branch information
megargayu committed Oct 1, 2022
1 parent e32d423 commit a0b8603
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 0 deletions.
95 changes: 95 additions & 0 deletions codechef/AMONGUS2/AMONGUS2.cpp
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.
72 changes: 72 additions & 0 deletions codeforces/problems/concomp/concomp.cpp
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.
14 changes: 14 additions & 0 deletions other/laneswitch/laneswitch.cpp
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;
}

0 comments on commit a0b8603

Please sign in to comment.