求问这份60分代码剩下的问题只有高精度了吗
查看原帖
求问这份60分代码剩下的问题只有高精度了吗
152652
AndyChen2005121楼主2020/12/9 23:23
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
int n, m;
vector<long long> g[100005];
long long in[100005], out[100005];
struct node {
	long long a, b;
	node(){
		a = 0;
		b = 1;
	}
	node(long long aa, long long bb){
		a = aa;
		b = bb;
	}
};
node w[100005];
int max(long long a, long long b){
	return a > b ? a : b;
}
int lcm(long long a, long long b){
	if(b==0) return a;
	return lcm(b, a % b);
}
int gcd(long long a, long long b){
	if(lcm(a,b)==1) return a*b;
	else return max(a, b);
}
node add(node x, node y){
	long long sum = x.b * y.b;
	long long ans = x.a * y.b + y.a * x.b;
	while(lcm(sum, ans) != 1){
		long long tmp = lcm(sum, ans);
		sum /= tmp;
		ans /= tmp;
	}
	return node(ans, sum);
}
node divide(node x, node y){
	long long ans = x.a * y.a;
	long long sum = x.b * y.b;
	while(lcm(sum, ans) != 1){
		long long tmp = lcm(sum, ans);
		sum /= tmp;
		ans /= tmp;
	}
	return node(ans, sum);
}
queue<long long> tmpq, q;
void topo() {
	for(int i = 1; i <= n; i++){
		if(in[i] <= 0){
			//cout << i << endl;
			tmpq.push(i);
			q.push(i);
			w[i].a = 1;
		}
	}
	while(!tmpq.empty()){
		long long now = tmpq.front();
		tmpq.pop();
		for(int i = 0; i < g[now].size(); i++){
			long long v = g[now][i];
			in[v]--;
			if(in[v] <= 0) {
				//cout << v << endl;
				if(out[v] > 0) q.push(v);
				tmpq.push(v);
			}
		}
	}
}
void bfs(){
	while(!q.empty()){
		long long cur = q.front();
		q.pop();
		node dividend(1, g[cur].size()); 
		node tmpnode = divide(w[cur], dividend);
		for(int i = 0; i < g[cur].size(); i++){
			long long next = g[cur][i];
			w[next] = add(w[next], tmpnode);
		}
	}
}
int main(){
//	freopen("water.in", "r", stdin);
//	freopen("water.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin >> n >> m;
	for(long long i = 1; i <= n; i++){
		long long x;
		cin >> x;
		for(long long j = 0; j < x; j++){
			long long v;
			cin >> v;
			g[i].push_back(v);
			in[v]++;
			out[i]++;
		}
	}
	topo();
	bfs();
	for(long long i = 1; i <= n; i++){
		if(out[i] <= 0){
			cout << w[i].a << " " << w[i].b << endl;
		}
	}
//	fclose(stdin);
//	fclose(stdout);
	return 0;
}

2020/12/9 23:23
加载中...