Logo Wy Online Judge

WyOJ

Paste!

#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>
#include <vector>

struct Graph{
	unsigned n;
	std::vector<unsigned>* table;
	void add_edge(unsigned node1, unsigned node2) {
		table[node1].push_back(node2);
		table[node2].push_back(node1);
	} 
	auto& operator[] (unsigned pos) const {
		return table[pos];
	}
	Graph(unsigned size)
		:n(size), table(new std::vector<unsigned>[size])
	{}
	~Graph() { delete[] table; }
}; 

int main() {
	using namespace std;
	int n, m;
	cin >> n >> m;
	Graph G(n);
	auto edges = new array<int, 2>[m];
	for(int i = 0; i < m; i++)
	{
		cin >> edges[i][0] >> edges[i][1];
		--edges[i][0];
		--edges[i][1];
		G.add_edge(edges[i][0], edges[i][1]);
	}
	vector mat(n, vector<_Float128>(n + 1, 0));
	mat[0][n] = 1;
	for(int i = 0; i < n; i++)
	{
		mat[i][i] = 1;
		if(i != n - 1)
			for(auto to : G[i])
				mat[i][to] -= 1. / G[i].size();
	}
	auto solve = [&] {
		int row = 0;
		for(int col = 0; col < n; col++)
		{
			int cur = row;
			for(int i = row + 1; i < n; i++)
				if(abs(mat[i][col]) > abs(mat[cur][col]))
					cur = i;
			mat[cur].swap(mat[row]);
			double mul = mat[row][col];
			for(auto& i : mat[row])
				i /= mul;
			for(int i = 0; i < n; i++)
				if(i != row)
				{
					mul = mat[i][col];
					for(int j = 0; j <= n; j++)
						mat[i][j] -= mat[row][j] * mul;
				}
			++row;
		}
	};
	solve();
	auto val = new double[m];
	for(int i = 0; i < m; i++)
		val[i] = mat[edges[i][0]][n] / G[edges[i][0]].size() +
			mat[edges[i][1]][n] / G[edges[i][1]].size();
	sort(val, val + m, greater());
	long double ans = 0, cur = 0;
	for(int i = 0; i < m; i++)
		ans += val[i] * ++cur;
	delete[] val;
	cout << fixed << setprecision(3) << ans << endl;
	return 0;
}