Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <cmath>
- #include <climits>
- using namespace std;
- struct puperfunctionmycreateion{
- int data, prev, st, ans;
- };
- int main()
- {
- puperfunctionmycreateion temp, temp1;
- int n, m, free = -1, v, u, h;
- vector <int> a, head, ans, next, dest;
- queue <puperfunctionmycreateion> q;
- cin >> n >> m;
- head.reserve(100000);
- a.reserve(100000);
- ans.reserve(100000);
- dest.reserve(100000);
- next.reserve(100000);
- head.resize(n);
- ans.resize(n);
- a.resize(n);
- for (int i = 1; i <= n; i++){
- next[i] = 0;
- ans[i] = INT_MAX - 20;
- cin >> a[i];
- if (a[i] != 0){
- temp.data = i;
- temp.st = i;
- temp.ans = 0;
- temp.prev = 0;
- q.push(temp);
- }
- }
- for (int i = 1; i <= m; i++){
- cin >> v >> u;
- free++;
- dest.resize(free + 1);
- next.resize(free + 1);
- dest[free] = u;
- next[free] = head[v];
- head[v] = free;
- free++;
- dest[free] = v;
- next[free] = head[u];
- head[u] = free;
- }
- h = head[3];
- while (h){
- cout << " " << dest[h] << endl;
- h = next[h];
- }
- cin >> h;
- while (!q.empty()){
- temp = q.front();
- h = head[temp.data];
- while (h){
- if (dest[h] != temp.prev)
- if (a[dest[h]] == 0){
- temp1.data = dest[h];
- temp1.st = temp.st;
- temp1.ans = temp.ans + 1;
- temp1.prev = temp.data;
- q.push(temp1);
- }
- else
- if (a[dest[h]] != a[temp.st]){
- //cout << q.size() << endl;
- ans[dest[h]] = min(ans[dest[h]], temp.ans + 1);
- }
- h = next[h];
- cout << " " << q.size() << endl;
- }
- cout << q.size() << endl;
- q.pop();
- }
- int min2;
- min2 = ans[1];
- for (int i = 2; i <= n; i++)
- min2 = min(min2, ans[i]);
- cout << min2 << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement