Skip to content

Instantly share code, notes, and snippets.

@amphineko
Created August 19, 2014 15:43
Show Gist options
  • Save amphineko/de0be0400f2fd50dc16d to your computer and use it in GitHub Desktop.
Save amphineko/de0be0400f2fd50dc16d to your computer and use it in GitHub Desktop.
/*
Luna Capsule / vijos-1022
Problem: https://vijos.org/p/1022
Record: https://vijos.org/records/53f36e2c48c5fc437d8b4574
- Create adjacency list (double-way) with the input,
BFS nodes and mark them, skip visited node when selecting the next node for BFSing.
*/
#include <cstdio>
unsigned n, a[201][201];
bool visited[201];
inline void registerLink(unsigned v1, unsigned v2) { a[v1][++a[v1][0]] = v2, a[v2][++a[v2][0]] = v1; }
void preprocess() {
unsigned i, j, t;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
a[i][j] = false;
a[i][0] = 0;
}
for (i = 1; i <= n; i++) {
scanf("%d", &t);
visited[i] = false;
while (t != 0)
registerLink(i, t), scanf("%d", &t);
}
}
void search(unsigned v0) {
unsigned queue[201], qi = 0, ql = 0, c, i;
queue[0] = v0, visited[v0] = true;
while (qi <= ql) {
c = queue[qi];
if (a[c][0] > 0)
for (i = 1; i <= n; i++)
if (!visited[a[c][i]])
queue[++ql] = a[c][i], visited[a[c][i]] = true;
qi++;
}
}
void process() {
unsigned i, t = 0;
for (i = 1; i <= n; i++)
if (!visited[i])
search(i), t++;
printf("%d", t);
}
int main() {
preprocess();
process();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment