Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <math.h>
- using namespace std;
- vector <vector<int>> g;
- int index = 1;
- int n;
- vector<bool> flag;
- int counter = 0;
- vector<int> a;
- int ans = 0;
- void getline(int i, string& s) {
- int j = 0;
- while (s[index] != ']') {
- if (isdigit(s[index])) {
- if (s[index] == '1')
- g[i].push_back(j);
- j++;
- }
- index++;
- }
- }
- void getvec(string& s, int n) {
- for (int i = 0; i < n; i++) {
- getline(i, s);
- index += 3;
- }
- }
- int getlen(string& s) {
- int n = 0;
- int ind = 1;
- while (s[ind] != ']') {
- if (isdigit(s[ind]))
- ++n;
- ind++;
- }
- return n;
- }
- void dfs(int v) {
- flag[v] = true;
- ++counter;
- for (int u : g[v]) {
- if (!flag[u] && u != v) {
- dfs(u);
- }
- }
- }
- int main() {
- string s;
- getline(cin, s);
- s = s.substr(2, s.size() - 3);
- int m = s[s.size() - 1] - '0';
- n = getlen(s);
- flag.resize(n);
- g.resize(n);
- s = s.substr(0, s.size() - 3);
- getvec(s, n);
- for (int i = 0; i < n; i++) {
- if (!flag[i]) {
- counter = 0;
- dfs(i);
- if (counter == 1)
- ++ans;
- else
- a.push_back(counter);
- }
- }
- for (int i = 0; i < a.size(); i++)
- ans += ceil(1.0 * a[i] / m);
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement