Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <string>
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #include <cmath>
- #include <climits>
- // #include <bits/stdc++.h>
- using namespace std;
- struct Point {
- int n;
- vector<Point*> paths;
- };
- int n, m;
- string s;
- vector<Point> farms;
- vector<pair<int, int> > friendpaths;
- vector<char> favoritemilktype;
- vector<int> result;
- bool friendinpath(int friend_, int a, int b) {
- queue<Point*> notebook;
- vector<bool> visited(n, false);
- notebook.push(&farms[a]);
- visited[a] = true;
- vector<int> path;
- while (!notebook.empty()) {
- Point* current = notebook.front();
- notebook.pop();
- for (int i = 0; i < current->paths.size(); i++) {
- if (!visited[current->paths[i]->n]) {
- visited[current->paths[i]->n] = true;
- notebook.push(current->paths[i]);
- path.push_back(current->paths[i]->n);
- }
- }
- path.pop_back();
- }
- return false;
- }
- int main(void) {
- cin >> n >> m;
- cin >> s;
- int a, b;
- farms.resize(n);
- for (int i = 0; i < n; i++) {
- farms[i].n = i;
- }
- for (int i = 0; i < n-1; i++) {
- cin >> a >> b;
- farms[a-1].paths.push_back(&farms[b-1]);
- farms[b-1].paths.push_back(&farms[a-1]);
- }
- char tmp;
- for (int i = 0; i < m; i++) {
- cin >> a >> b >> tmp;
- friendpaths.push_back(make_pair(a-1, b-1));
- favoritemilktype.push_back(tmp);
- }
- // cout << "n m:" << endl;
- // cout << n << " " << m << endl;
- // cout << "s:" << endl;
- // cout << s << endl;
- // cout << "farms:" << endl;
- // for (int i = 0; i < n; i++) {
- // for (int j = 0; j < farms[i].paths.size(); j++) {
- // cout << farms[i].paths[j]+1 << " ";
- // }
- // cout << endl;
- // }
- // cout << "friendpaths:" << endl;
- // for (int i = 0; i < m; i++) {
- // cout << friendpaths[i].first+1 << " " << friendpaths[i].second+1 << " " << favoritemilktype[i] << endl;
- // }
- // if (friendinpath(0, 0, 1)) cout << "YES" << endl; else cout << "NO" << endl;
- for (int i = 0; i < m; i++) {
- if (friendinpath(i, friendpaths[i].first, friendpaths[i].second)) {
- result.push_back(1);
- } else {
- result.push_back(0);
- }
- }
- for (int i = 0; i < result.size(); i++) {
- cout << result[i];
- }
- cout << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement