P7113 [NOIP2020] drainage system (DFS) (90Point simple algorithm)

Title Description For a city, drainage system...
Title Description
Input format
Output format
Input and output samples

Title Description

For a city, drainage system is an extremely important part.

One day, little C got the design drawing of a city's drainage system. The drainage system consists of   nn   Drainage nodes (they are from   1 \sim n1∼n   Number) and several one-way drainage pipes. Each drainage node has several pipelines for collecting the sewage of other drainage nodes (hereinafter referred to as the collection pipeline of the node), and several pipelines also discharge the sewage to other drainage nodes (hereinafter referred to as the discharge pipeline of the node).

The nodes of the drainage system are   mm   There are two sewage receiving ports, and their numbers are   1, 2, \ ldots, M1, 2,..., m, sewage can only flow into the drainage system from these receiving ports, and there is no collecting pipe at these nodes. There are several final outlets in the drainage system, which transport the sewage to the sewage treatment plant. The node without discharge pipeline can be regarded as a final outlet.

Now each sewage receiving port receives sewage separately   eleven   Tons of sewage. After entering each node, the sewage will flow equally from each discharge pipe of the current node to other drainage nodes, and the final outlet will discharge the sewage into the system.

Now little C wants to know how much sewage will be discharged from each final outlet in the city's drainage system. The drainage system of the city is designed scientifically, and the pipeline will not form a loop, that is, the sewage will not form a circulation.

Input format

The first two integers separated by a single space   n, mn,m. Indicates the number of drainage nodes and the number of receiving ports respectively.
next   nn   OK, No   ii   Lines are used to describe nodes   ii   All discharge pipes. Where the first integer in each line   d_idi​   Indicates the number of discharge pipes, and then   d_idi​   An integer separated by a single space   a_1, a_2, \ldots , a_a1​,a2​,…,adi​​   Represent the target drainage node of the pipeline in turn.
Ensure that there will not be two pipes with the same starting node and target node.

Output format

Output several lines and give the volume of sewage discharged from each final outlet in the order of number from small to large. The volume is output in fractional form, that is, two integers separated by a single space are output per line   PP, QQ, indicating that the volume of discharged sewage is  \ fracqp​. requirement   pp   And   qq   Coprime, q = 1q=1   Output is also required   qq.

Input and output samples

Enter #1 copy

5 1 3 2 3 5 2 4 5 2 5 4 0 0

Output #1 copy

1 3 2 3

The code is as follows. I passed 90 points in Luogu. The last one should be because the unsigned long range burst when passing the points. Readers have optimized full marks or ideas for my code. Welcome to write to me privately. Thank you for your support.

My code uses DFS. The first time I wrote the code for an hour and a half, I passed 7 points and got 80 points. Later, I made a big optimization. All kinds of high precision and small skills were used, and finally got 90 points. The code I passed for the first time is also attached below.

90 Point Code:
#include<cstdio> #include<cstring> int map[100005][7]; int ru[100005]; unsigned long long n,m,x,y,xia,shang,kuoa,kuob,r,rr,res,left,right; struct fenshu{ unsigned long long x; unsigned long long y; fenshu(){ x=0; y=0; } //It seems to be the standard of c99 }; fenshu qingkong; fenshu num[100005]; //All defined variables inline unsigned long long read(){ char c; int f=1; res=0; c=getchar(); while(c<'0' || c>'9')c=getchar(); while(c>='0' && c<='9'){ res=(res<<1)+(res<<3)+(c^48); c=getchar(); } return res; } inline unsigned long long gcd(unsigned long long a,unsigned long long b) { if(b==0) return a; return gcd(b,a%b); } inline fenshu jia(fenshu a,fenshu b){ fenshu nnn; if(b.x==0 || b.y==0){ xia=a.y; shang=a.x; } else if(a.y==b.y){ shang=a.x+b.x; xia=a.y; r=gcd(xia,shang); shang=shang/r; xia=xia/r; } else{ rr=gcd(a.y,b.y); kuoa=b.y/rr; kuob=a.y/rr; shang=(kuoa*a.x+kuob*b.x); //Optimization should be done here, otherwise the same branch will cross the unsigned long long boundary r=gcd(a.y,rr); left=a.y/r; rr=rr/r; r=gcd(b.y,rr); right=b.y/r; rr=rr/r; if(left%rr==0){ left=left/rr; xia=left*right; } else if(right%rr==0){ right=right/rr; xia=left*right; } else xia=(left*right)/rr; r=gcd(xia,shang); if(r!=1){ xia=xia/r; shang=shang/r; } } nnn.x=shang; nnn.y=xia; return nnn; } inline fenshu fen(fenshu goal,unsigned long long piece){ if(piece==1){ return goal; } else{ fenshu nn; rr=gcd(goal.x,piece); if(rr!=1){ nn.x=goal.x/rr; piece=piece/rr; } else{ nn.x=goal.x; } nn.y=goal.y*piece; r=gcd(nn.x,nn.y); if(r!=1){ nn.x=nn.x/r; nn.y=nn.y/r; } return nn; } } inline void add_dfs(int start){ if(map[start][0]==0)return; fenshu fen_num; fen_num=fen(num[start],map[start][0]); for(int i=1;i<=map[start][0];i++){ num[map[start][i]]=jia(fen_num,num[map[start][i]]); add_dfs(map[start][i]); num[start]=qingkong; } } signed main(){ freopen("rr.ii","r",stdin); freopen("rr.out","w",stdout); qingkong.x=0; qingkong.y=0; memset(map,0,sizeof(map)); memset(ru,0,sizeof(ru)); n=read();m=read(); for(int i=1;i<=n;i++){ x=read(); for(int ii=1;ii<=x;ii++){ //If 0, return y=read(); ru[y]++; map[i][0]++; map[i][map[i][0]]=y; } } for(int i=1;i<=n;i++){ if(ru[i]==0){ num[i].x=1; num[i].y=1; add_dfs(i); } } for(int i=1;i<=n;i++) if(map[i][0]==0) printf("%llu %llu\n",num[i].x,num[i].y); //Note format return 0; }

The test data are as follows  

First Code(80)
#include<cstdio> #include<cstring> int map[100005][7]; int ru[100005]; struct fenshu{ unsigned long long x; unsigned long long y; fenshu(){ x=0; y=0; } }; fenshu qingkong; fenshu num[100005]; inline unsigned long long read(){ char c; int f=1; long long res=0; c=getchar(); while(c<'0' || c>'9')c=getchar(); while(c>='0' && c<='9'){ res=(res<<1)+(res<<3)+(c^48); c=getchar(); } return res; } inline long long gcd(unsigned long long a,unsigned long long b) { if(b==0) return a; return gcd(b,a%b); } inline fenshu jia(fenshu a,fenshu b){ fenshu nnn; unsigned long long r; unsigned long long xia; unsigned long long shang; if(b.x==0 || b.y==0){ xia=a.y; shang=a.x; } else{ xia=(a.y*b.y); shang=(a.x*b.y+a.y*b.x); r=gcd(xia,shang); if(r!=1 && r!=0){ xia=xia/r; shang=shang/r; } } nnn.x=shang; nnn.y=xia; return nnn; } inline fenshu fen(fenshu goal,long long piece){ fenshu nn; if(piece==1){ return goal; } else{ nn.x=goal.x; nn.y=goal.y*piece; unsigned long long r=gcd(nn.x,nn.y); if(r!=1 && r!=0){ nn.x=nn.x/r; nn.y=nn.y/r; } } return nn; } inline void add_dfs(int start){ if(map[start][0]==0)return; fenshu fen_num; fen_num=fen(num[start],map[start][0]); for(int i=1;i<=map[start][0];i++){ num[map[start][i]]=jia(fen_num,num[map[start][i]]); add_dfs(map[start][i]); num[start]=qingkong; } } signed main(){ qingkong.x=0; qingkong.y=0; memset(map,0,sizeof(map)); memset(ru,0,sizeof(ru)); unsigned long long n,m,x,y; n=read(); m=read(); for(int i=1;i<=n;i++){ x=read(); for(int ii=1;ii<=x;ii++){ //If 0, return y=read(); ru[y]++; map[i][0]++; map[i][map[i][0]]=y; } } for(int i=1;i<=n;i++){ if(ru[i]==0){ num[i].x=1; num[i].y=1; add_dfs(i); } } for(int i=1;i<=n;i++){ if(map[i][0]==0){ printf("%lld %lld\n",num[i].x,num[i].y); } } return 0; }

19 October 2021, 21:31 | Views: 6929

Add new comment

For adding a comment, please log in
or create account

0 comments