#include <iostream> #include <vector> #include <queue> #include <climits> using namespace std; long long locate(vector<vector<int>>& G, vector<int>& dist, int fir, int sec) { long long res = 0; vector<int> order; queue<int> Q; dist[fir] = 0; dist[sec] = 0; Q.push(fir); Q.push(sec); int curr; while (!Q.empty()) { curr = Q.front(); Q.pop(); order.push_back(curr); for (int i = 0; i < G[curr].size(); i++) { if (dist[G[curr][i]] == INT_MAX / 4) { dist[G[curr][i]] = dist[curr] + 1; } } } for (int i = 0; i < order.size(); i++) { for (int j = 0; j < G[order[i]].size(); j++) { if (dist[G[order[i]][j]] > dist[order[i]]) { res += dist[order[i]] + 1; } } } return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int zarowki, przelaczniki; cin >> zarowki >> przelaczniki; vector<bool> Z(zarowki); bool S; for (int i = 0; i < zarowki; i++) { cin >> S; Z[i] = S; } vector<vector<int>> G(zarowki); int a, b; vector<int> dist(zarowki, INT_MAX / 4); for (int i = 0; i < przelaczniki; i++) { cin >> a >> b; G[a - 1].push_back(b - 1); G[b - 1].push_back(a - 1); } vector<long long> wyniki; for (int i = 0; i < zarowki; i++) { for (int j = 0; j < G[i].size(); j++) { if (dist[G[i][j]] == INT_MAX/4 && Z[i] == Z[G[i][j]]) { wyniki.push_back(2 + locate(G, dist, i, G[i][j])); } } } long long res = 1; for (int i = 0; i < wyniki.size(); i++) { res *= wyniki[i]; res %= 1000000007; } cout << res << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <iostream> #include <vector> #include <queue> #include <climits> using namespace std; long long locate(vector<vector<int>>& G, vector<int>& dist, int fir, int sec) { long long res = 0; vector<int> order; queue<int> Q; dist[fir] = 0; dist[sec] = 0; Q.push(fir); Q.push(sec); int curr; while (!Q.empty()) { curr = Q.front(); Q.pop(); order.push_back(curr); for (int i = 0; i < G[curr].size(); i++) { if (dist[G[curr][i]] == INT_MAX / 4) { dist[G[curr][i]] = dist[curr] + 1; } } } for (int i = 0; i < order.size(); i++) { for (int j = 0; j < G[order[i]].size(); j++) { if (dist[G[order[i]][j]] > dist[order[i]]) { res += dist[order[i]] + 1; } } } return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int zarowki, przelaczniki; cin >> zarowki >> przelaczniki; vector<bool> Z(zarowki); bool S; for (int i = 0; i < zarowki; i++) { cin >> S; Z[i] = S; } vector<vector<int>> G(zarowki); int a, b; vector<int> dist(zarowki, INT_MAX / 4); for (int i = 0; i < przelaczniki; i++) { cin >> a >> b; G[a - 1].push_back(b - 1); G[b - 1].push_back(a - 1); } vector<long long> wyniki; for (int i = 0; i < zarowki; i++) { for (int j = 0; j < G[i].size(); j++) { if (dist[G[i][j]] == INT_MAX/4 && Z[i] == Z[G[i][j]]) { wyniki.push_back(2 + locate(G, dist, i, G[i][j])); } } } long long res = 1; for (int i = 0; i < wyniki.size(); i++) { res *= wyniki[i]; res %= 1000000007; } cout << res << endl; return 0; } |