对没错我就是在水博
P3366 【模板】最小生成树
kruskal:
P3367 【模板】并查集
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=210000; 9 int read(){ 10 int x=0,f=1;char ch=getchar(); 11 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 12 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 13 return x*f; 14 } 15 int n,m; 16 int fa[mxn]; 17 void init(){ 18 for(int i=1;i<=n;i++){ 19 fa[i]=i; 20 } 21 } 22 int find(int x){ 23 if(fa[x]==x)return x; 24 return fa[x]=find(fa[x]); 25 } 26 int main(){ 27 n=read();m=read(); 28 init(); 29 int x,y,z; 30 while(m--){ 31 z=read();x=read();y=read(); 32 x=find(x); 33 y=find(y); 34 if(z==1){ 35 fa[x]=y; 36 } 37 else{ 38 if(x==y){ 39 printf("Y\n"); 40 } 41 else printf("N\n"); 42 } 43 } 44 return 0; 45 }
P3371 【模板】单源最短路径
水这个模板题的时候,邻接表数组开小了,WA了五六次,悲伤。
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxx=2147483647; 9 const int mxn=540000; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 13 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 14 return x*f; 15 } 16 struct edge{ 17 int v,dis; 18 int nxt; 19 }e[mxn]; 20 int hd[mxn],mct=0; 21 void add_edge(int u,int v,int dis){ 22 e[++mct].v=v;e[mct].dis=dis;e[mct].nxt=hd[u];hd[u]=mct; 23 return; 24 } 25 int n,m,s; 26 int dis[mxn]; 27 bool inq[mxn]; 28 int q[360010]; 29 int vis[mxn]; 30 int head=0,tl=1; 31 void spfa(){ 32 for(int i=1;i<=n;i++){dis[i]=mxx;} 33 dis[s]=0; 34 q[++head]=s; 35 while(head!=tl+1){ 36 int u=q[head];head=(head+1)%360000;inq[u]=0; 37 for(int i=hd[u];i;i=e[i].nxt){ 38 int v=e[i].v; 39 if((long long)dis[u]+(long long)e[i].dis<(long long)dis[v]){ 40 dis[v]=dis[u]+e[i].dis; 41 vis[v]++; 42 if(vis[v]>n)return; 43 if(!inq[v]){ 44 inq[v]=1; 45 tl=(tl+1)%360000; 46 q[tl]=v; 47 } 48 } 49 } 50 } 51 return; 52 } 53 int main(){ 54 n=read();m=read();s=read(); 55 int i,j; 56 int u,v,d; 57 for(i=1;i<=m;i++){ 58 u=read(); 59 v=read(); 60 d=read(); 61 add_edge(u,v,d); 62 } 63 spfa(); 64 for(i=1;i<=n;i++){ 65 printf("%d ",dis[i]); 66 } 67 printf("\n"); 68 return 0; 69 }
P3383 【模板】线性筛素数
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 int read(){ 9 int x=0,f=1;char ch=getchar(); 10 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 11 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 12 return x*f; 13 } 14 const int mxn=10000005; 15 int pri[mxn>>1],cnt; 16 bool vis[mxn]; 17 void PRI(int mxn){ 18 vis[1]=1; 19 int i,j; 20 for(i=2;i<mxn;i++){ 21 if(!vis[i])pri[++cnt]=i; 22 for(j=1;j<=cnt && i*pri[j]<mxn;j++){ 23 vis[i*pri[j]]=1; 24 if(i%pri[j]==0)break; 25 } 26 } 27 return; 28 } 29 int n,m; 30 int main(){ 31 n=read();m=read(); 32 PRI(n+3); 33 int x; 34 while(m--){ 35 x=read(); 36 if(vis[x])printf("No\n"); 37 else printf("Yes\n"); 38 } 39 return 0; 40 }
时间: 2024-10-05 04:03:13