Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <cassert>
- using namespace std;
- typedef long double ld;
- const int Maxn = 16;
- const int Maxm = 200;
- const ld eps = 1e-11l;
- int t;
- int n, m;
- vector <int> neigh[Maxn];
- int a, b;
- int id[Maxn][Maxn];
- int curcomp, comp[Maxn];
- ld matr[Maxm][Maxm];
- ld res[Maxm];
- void getComp(int v, int col)
- {
- if (comp[v]) return;
- comp[v] = col;
- for (int i = 0; i < neigh[v].size(); i++) {
- int u = neigh[v][i];
- getComp(u, col);
- }
- }
- int ID(int a, int b) { return id[min(a, b)][max(a, b)]; }
- int main()
- {
- scanf("%d", &t);
- for (int tc = 1; tc <= t; tc++) {
- scanf("%d %d", &n, &m);
- for (int i = 1; i <= n; i++) {
- neigh[i].clear();
- neigh[i].push_back(i);
- comp[i] = 0;
- }
- for (int i = 1; i <= m; i++) {
- int a, b; scanf("%d %d", &a, &b);
- neigh[a].push_back(b);
- neigh[b].push_back(a);
- }
- scanf("%d %d", &a, &b);
- curcomp = 0;
- for (int i = 1; i <= n; i++)
- if (comp[i] == 0) getComp(i, ++curcomp);
- printf("Case #%d: ", tc);
- if (comp[a] != comp[b])
- for (int i = 1; i <= n; i++)
- printf("N/A%c", i + 1 <= n? ' ': '\n');
- else {
- int cur = 0;
- for (int i = 1; i <= n; i++) if (comp[i] == comp[a])
- for (int j = i; j <= n; j++) if (comp[j] == comp[a])
- id[i][j] = cur++;
- for (int s = 1; s <= n; s++) {
- if (comp[s] != comp[a]) printf("N/A");
- else {
- for (int i = 1; i <= n; i++) if (comp[i] == comp[a])
- for (int j = i; j <= n; j++) if (comp[j] == comp[a]) {
- for (int k = 0; k <= cur; k++)
- matr[id[i][j]][k] = 0;
- matr[id[i][j]][id[i][j]] = 1;
- if (i != s || j != s) {
- matr[id[i][j]][cur] = 1;
- ld hm = 1.0l / (ld(neigh[i].size()) * ld(neigh[j].size()));
- for (int c = 0; c < neigh[i].size(); c++) {
- int ni = neigh[i][c];
- for (int d = 0; d < neigh[j].size(); d++) {
- int nj = neigh[j][d];
- matr[id[i][j]][ID(ni, nj)] -= hm;
- }
- }
- }
- }
- for (int j = 0; j < cur; j++) {
- int i = j;
- while (i < cur && fabs(matr[i][j]) < eps) i++;
- assert(i < cur);
- if (i != j)
- for (int k = 0; k <= cur; k++)
- swap(matr[i][k], matr[j][k]);
- i = j;
- for (int i2 = i + 1; i2 < cur; i2++)
- if (fabs(matr[i2][j]) >= eps) {
- ld mult = -matr[i2][j] / matr[i][j];
- for (int k = 0; k <= cur; k++)
- matr[i2][k] += mult * matr[i][k];
- }
- }
- for (int i = cur - 1; i >= 0; i--) {
- ld val = matr[i][cur];
- for (int j = i + 1; j < cur; j++)
- val -= matr[i][j] * res[j];
- res[i] = val / matr[i][i];
- if (i == ID(a, b)) {
- cout << fixed << setprecision(12) << res[i];
- break;
- }
- }
- }
- printf("%c", s + 1 <= n? ' ': '\n');
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement