Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- #include <fstream>
- using namespace std;
- const int maxn = 1e5 + 10;
- vector<int> graph[maxn];
- int main() {
- ifstream cin("milkvisits.in");
- ofstream cout("milkvisits.out");
- int n, m;
- cin >> n >> m;
- string s;
- cin >> s;
- for(int i = 1; i < n; i++) {
- int a, b;
- cin >> a >> b;
- a--;
- b--;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- vector<pair<pair<int, int>, char>> queries;
- for(int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- a--; b--;
- char c;
- cin >> c;
- queries.push_back(make_pair(make_pair(a, b), c));
- }
- vector<int> id(n, -1);
- for(int i = 0; i < n; i++) {
- if(id[i] == -1) {
- queue<int> q;
- q.push(i);
- while(!q.empty()) {
- int node = q.front();
- q.pop();
- id[node] = i;
- for(int neighbour : graph[node]) {
- if(id[neighbour] == -1 and s[neighbour] == s[i]) {
- q.push(neighbour);
- }
- }
- }
- }
- }
- string res = "";
- for(int i = 0; i < m; i++) {
- int a = queries[i].first.first;
- int b = queries[i].first.second;
- char c = queries[i].second;
- if(id[a] == id[b]) {
- if(s[a] == c) {
- res += "1";
- }
- else {
- res += "0";
- }
- }
- else {
- res += "1";
- }
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement