团体程序设计天梯赛-练习集
团体程序设计天梯赛-练习集
题目链接:https://www.patest.cn/contests/gplt
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 using namespace std; 5 int main(void){ 6 int n; 7 char ch; 8 scanf("%d %c",&n,&ch); 9 int floor=sqrt((n+1.0)/2); 10 int res=n-2*floor*floor+1; 11 for(int i=floor;i>=1;--i){ 12 for(int j=0;j<floor-i;++j) 13 printf(" "); 14 for(int j=0;j<2*i-1;++j) 15 printf("%c",ch); 16 printf("\n"); 17 } 18 for(int i=2;i<=floor;++i){ 19 for(int j=0;j<floor-i;++j) 20 printf(" "); 21 for(int j=0;j<2*i-1;++j) 22 printf("%c",ch); 23 printf("\n"); 24 } 25 printf("%d\n",res); 26 return 0; 27 }
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int a[10]; 5 char s[1005]; 6 int main(void){ 7 scanf("%s",s); 8 for(int i=0;i<strlen(s);i++)a[s[i]-'0']++; 9 for(int i=0;i<10;i++)if(a[i])printf("%d:%d\n",i,a[i]); 10 return 0; 11 }
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 struct S{ 5 char id[15]; 6 int a,b; 7 }; 8 bool complare(S x,S y){ 9 return x.a<y.a; 10 } 11 S q[1000]; 12 int main(void){ 13 int n; 14 scanf("%d",&n); 15 for(int i=0;i<n;++i) 16 scanf("%s%d%d",q[i].id,&q[i].a,&q[i].b); 17 sort(q,q+n,complare); 18 int m; 19 S tmp,t; 20 scanf("%d",&m); 21 for(int i=0;i<m;++i){ 22 scanf("%d",&t.a); 23 t=*(lower_bound(q,q+n,t,complare));/**t?!**/ 24 printf("%s %d\n",t.id,t.b); 25 } 26 return 0; 27 }
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 #define MAX_N 1000 6 #define LL long long 7 using namespace std; 8 int a[MAX_N]; 9 int main(void){ 10 int n; 11 //freopen("in.txt","r",stdin); 12 freopen("in.txt","r",stdin); 13 freopen("out.txt","w",stdout); 14 while(~scanf("%d",&n)){ 15 memset(a,0,sizeof(a)); 16 int k=0; 17 int start=0; 18 int limit=(int)sqrt(n); 19 for(int i=2;i<=limit;++i) 20 if(n%i==0) 21 a[k++]=i; 22 a[k++]=n; 23 int len=1,temp=1,l=0; 24 int store=a[0]; 25 for(int i=1;i<k;++i){ 26 if(a[i]==a[i-1]+1){//因数应当不重复使用 27 if(!(n%(store*a[i]))&&store*a[i]<=n){ 28 store*=a[i]; 29 temp++; 30 }else if(!(n%((store/a[l])*a[i]))&&store/a[l]*a[i]<=n){ 31 store/=a[l]; 32 l++; 33 store*=a[i]; 34 }else{ 35 store=a[i]; 36 temp=1; 37 l=i; 38 } 39 }else{ 40 store=a[i]; 41 temp=1; 42 l=i; 43 } 44 if(temp>len){ 45 len=temp; 46 start=l; 47 } 48 } 49 printf("%d\n%d",len,a[start]); 50 for(int i=start+1;i<start+len;++i) 51 printf("*%d",a[i]); 52 printf("\n");} 53 return 0; 54 }
1 #include<cstdio> 2 #include<string> 3 #include<iostream> 4 #include<cstring> 5 using namespace std; 6 char a[200]; 7 int main(void){ 8 string b[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; 9 scanf("%s",a); 10 int len=strlen(a); 11 if(a[0]=='-')printf("fu"); 12 else cout<<b[a[0]-'0']; 13 for(int i=1;i<len;++i) 14 cout<<' '<<b[a[i]-'0']; 15 printf("\n"); 16 return 0; 17 }
1 #include<cstdio> 2 using namespace std; 3 int main(void){ 4 int a,b; 5 scanf("%d%d",&a,&b); 6 int sum=0,k=0; 7 for(int i=a;i<=b;++i){ 8 printf("%5d",i); 9 k++; 10 sum+=i; 11 if(!(k%5))printf("\n"); 12 } 13 if(k%5)printf("\n"); 14 printf("Sum = %d\n",sum); 15 return 0; 16 }
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #define LL long long 5 using namespace std; 6 LL a[101],b[101]; 7 LL gcd(LL x,LL y){ 8 return y==0?x:gcd(y,x%y); 9 } 10 void simple(LL i){ 11 LL temp=gcd(abs(a[i]),b[i]); 12 a[i]/=temp; 13 b[i]/=temp; 14 } 15 int main(void){ 16 //freopen("1.in.txt","r",stdin); 17 LL n; 18 scanf("%lld",&n); 19 for(LL i=0;i<n;++i){ 20 scanf("%lld/%lld",&a[i],&b[i]); 21 simple(i); 22 } 23 long long x=a[0],y=b[0]; 24 for(LL i=1;i<n;i++){ 25 x=x*b[i]+y*a[i]; 26 y=y*b[i]; 27 if(x){ 28 LL temp=gcd(abs(x),y); 29 x/=temp; 30 y/=temp; 31 } 32 } 33 if(!x){ 34 printf("0\n"); 35 return 0; 36 } 37 LL temp=x/y; 38 x=x-temp*y; 39 LL t=gcd(abs(x),y); 40 x/=t; 41 y/=t; 42 if(temp){ 43 printf("%lld",temp); 44 if(x)printf(" %lld/%lld",x,y); 45 printf("\n"); 46 }else{ 47 if(x)printf("%lld/%lld",x,y); 48 printf("\n"); 49 } 50 return 0; 51 }
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 int main(void){ 5 int a[3]; 6 for(int i=0;i<3;++i) 7 scanf("%d",&a[i]); 8 sort(a,a+3); 9 printf("%d->%d->%d\n",a[0],a[1],a[2]); 10 return 0; 11 }
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<iostream> 5 #define N 10000 6 #define LL long long 7 using namespace std; 8 char a[N+1],b[N+1],s[N+1]; 9 bool sb[300]; 10 int main(void){ 11 //freopen("1.in.txt","r",stdin); 12 gets(a); 13 gets(b); 14 LL len=strlen(b); 15 for(LL i=0;i<len;++i) 16 sb[b[i]]=true; 17 len=strlen(a); 18 LL k=0; 19 for(LL i=0;i<len;++i) 20 if(!sb[a[i]])s[k++]=a[i]; 21 cout<<s<<endl; 22 return 0; 23 }
1 #include<cstdio> 2 using namespace std; 3 int main(void){ 4 int n; 5 scanf("%d",&n); 6 int temp=1; 7 for(int i=1;i<=n;++i) 8 temp*=2; 9 printf("2^%d = %d\n",n,temp); 10 return 0; 11 }
1 #include<cstdio> 2 #define LL long long 3 using namespace std; 4 int main(void){ 5 LL n; 6 scanf("%lld",&n); 7 LL temp=1; 8 LL sum=0; 9 for(LL i=1;i<=n;++i){ 10 temp=1; 11 for(LL j=1;j<=i;++j){ 12 temp*=j; 13 } 14 sum+=temp; 15 } 16 printf("%lld\n",sum); 17 return 0; 18 }
1 #include<cstdio> 2 using namespace std; 3 int main(void){ 4 int n; 5 char a; 6 scanf("%d %c",&n,&a); 7 for(int i=1;i<=((n+1)/2);++i){ 8 for(int j=1;j<=n;++j) 9 printf("%c",a); 10 printf("\n"); 11 } 12 return 0; 13 }
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int main(void){ 5 //freopen("in.txt","r",stdin); 6 char z[]={'1','0','X','9','8','7','6','5','4','3','2'}; 7 char a[19]; 8 int q[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; 9 int temp=0; 10 int n,m; 11 bool flat=false,k=true; 12 scanf("%d",&n); 13 while(n--){ 14 memset(a,0,sizeof(a)); 15 scanf("%s",a); 16 flat=false; 17 temp=0; 18 for(int i=0;i<17;++i){ 19 if(a[i]<'0'||a[i]>'9'){ 20 flat=true; 21 break; 22 } 23 temp=(temp+q[i]*(a[i]-'0'))%11; 24 } 25 if(flat){ 26 printf("%s\n",a); 27 k=false; 28 }else{ 29 if(z[temp]!=a[17]){ 30 printf("%s\n",a); 31 k=false; 32 } 33 } 34 } 35 if(k)printf("All passed\n"); 36 return 0; 37 }
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 using namespace std; 5 string s; 6 int bit,o=1,t; 7 double f=1; 8 int main(void){ 9 cin>>s; 10 for(int i=0;i<s.length();++i){ 11 if(s[i]=='-'){ 12 f+=0.5; 13 }else{ 14 if(s[i]=='2')t++; 15 bit++; 16 } 17 } 18 if((s[s.length()-1]-'0')%2==0)o+=1; 19 printf("%.2lf%%\n",1.0*t/bit*f*o*100); 20 }
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 char s[10]; 5 int h,m; 6 int get(int l,int r){ 7 int t=0; 8 for(int i=l;i<=r;++i) 9 t=t*10+s[i]-'0'; 10 return t; 11 } 12 int main(void){ 13 scanf("%s",s); 14 h=get(0,1);m=get(3,4); 15 if(0<=h&&h<=11)printf("Only %s. Too early to Dang.",s); 16 else if(h==12&&m==0)printf("Only %s. Too early to Dang.",s); 17 else{ 18 if(h>12)h-=12; 19 if(m)h+=1; 20 for(int i=0;i<h;++i) 21 printf("Dang"); 22 } 23 }
1 #include <iostream> 2 using namespace std; 3 int a,b,n,jia,yi; 4 int main(void){ 5 cin>>a>>b>>n; 6 while(n--){ 7 int x,xh,y,yh; 8 cin>>x>>xh>>y>>yh; 9 int t=x+y; 10 if(t==xh&&t==yh)continue; 11 else if(t==xh)jia++; 12 else if(t==yh)yi++; 13 if(jia>a){ 14 cout<<"A\n"<<yi; 15 return 0; 16 }else if(yi>b){ 17 cout<<"B\n"<<jia; 18 return 0; 19 } 20 } 21 }
1 #include <cstdio> 2 using namespace std; 3 int n,m,p[100000],ans[100000],vis[100000]; 4 int main(void){ 5 scanf("%d",&n); 6 while(n--){ 7 int k; 8 scanf("%d",&k); 9 int x; 10 for(int i=0;i<k;++i){ 11 scanf("%d",&x); 12 if(k<=1)continue; 13 p[x]=1; 14 } 15 } 16 scanf("%d",&m); 17 int k=0; 18 while(m--){ 19 int x; 20 scanf("%d",&x); 21 if(!p[x]&&!vis[x]){ 22 vis[x]=1; 23 ans[k++]=x; 24 } 25 } 26 if(!k)printf("No one is handsome\n"); 27 else{ 28 for(int i=0;i<k;++i) 29 printf("%05d%c",ans[i],i==k-1?'\n':' '); 30 } 31 }
1 #include <cstdio> 2 #include <cstring> 3 #define N 10005 4 using namespace std; 5 char a[N],b[N]; 6 int string2int(char a[]){ 7 int temp=0; 8 for(int i=0;i<strlen(a);++i){ 9 if(a[i]<'0'||a[i]>'9')return -1; 10 temp=temp*10+a[i]-'0'; 11 } 12 return temp<=1000&&temp>=1?temp:-1; 13 } 14 int main(void){ 15 scanf("%s%*c",a); 16 gets(b); 17 int x=string2int(a); 18 int y=string2int(b); 19 printf("%s + %s = ",x==-1?"?":a,y==-1?"?":b); 20 if(x!=-1&&y!=-1)printf("%d\n",x+y); 21 else puts("?"); 22 }
1 #include <iostream> 2 #include <queue> 3 #include <vector> 4 #define N 505 5 using namespace std; 6 const int inf=0x3fffffff; 7 int n,m,s,d,p[N],pre[N],dis[N],per[N],num[N]; 8 bool vis[N]; 9 struct edge{int to,w;}; 10 vector<edge>e[N]; 11 struct node{ 12 int u,d; 13 bool operator < (const node x)const{return d>x.d;} 14 }; 15 priority_queue<node>q; 16 void dij(int s){ 17 for(int i=0;i<n;++i)dis[i]=inf; 18 dis[s]=0;per[s]=p[s];num[s]=1;pre[s]=-1; 19 q.push((node){s,0}); 20 while(!q.empty()){ 21 node t=q.top();q.pop(); 22 int u=t.u; 23 if(vis[u])continue; 24 vis[u]=1; 25 for(int i=0;i<(int)e[u].size();++i){ 26 int v=e[u][i].to,w=e[u][i].w; 27 28 if(dis[u]+w==dis[v])num[v]+=num[u]; 29 if(dis[u]+w<dis[v])num[v]=num[u]; 30 31 if( (dis[u]+w==dis[v]&&per[u]+p[v]>per[v]) 32 ||dis[u]+w<dis[v]){ 33 dis[v]=dis[u]+w; 34 per[v]=per[u]+p[v]; 35 pre[v]=u; 36 q.push((node){v,dis[v]}); 37 } 38 } 39 } 40 } 41 void dfs(int k){ 42 if(k==-1)return; 43 dfs(pre[k]); 44 cout<<k<<" "; 45 } 46 int main(void){ 47 std::ios::sync_with_stdio(false); 48 cin>>n>>m>>s>>d; 49 for(int i=0;i<n;++i)cin>>p[i]; 50 for(int i=0;i<m;++i){ 51 int u,v,w; 52 cin>>u>>v>>w; 53 e[u].push_back((edge){v,w}); 54 e[v].push_back((edge){u,w}); 55 } 56 dij(s); 57 cout<<num[d]<<" "<<per[d]<<"\n"; 58 dfs(pre[d]); 59 cout<<d<<endl; 60 }
1 #include<cstdio> 2 #include<cstring> 3 #include<cstring> 4 #include<stack> 5 #include<cmath> 6 #define MAX_N 100000 7 using namespace std; 8 struct TYPE{ 9 int vul,next; 10 }; 11 TYPE a[MAX_N+1]; 12 int x[MAX_N+1],y[MAX_N+1]; 13 bool compare[MAX_N+1]; 14 int main(void){ 15 freopen("in.txt","r",stdin); 16 int adress,n; 17 scanf("%d%d",&adress,&n); 18 for(int i=0;i<n;++i){ 19 int temp; 20 scanf("%d",&temp); 21 scanf("%d%d",&a[temp].vul,&a[temp].next); 22 } 23 int k1=0,k2=0; 24 for(int i=adress;i!=-1;i=a[i].next){ 25 int temp=abs(a[i].vul); 26 if(compare[temp]==false){ 27 x[k1++]=i; 28 compare[temp]=true; 29 }else{ 30 y[k2++]=i; 31 } 32 } 33 printf("%05d %d ",x[0],a[x[0]].vul); 34 for(int i=1;i<k1;++i){ 35 printf("%05d\n",x[i]); 36 printf("%05d %d ",x[i],a[x[i]].vul); 37 } 38 printf("-1\n"); 39 if(k2){ 40 printf("%05d %d ",y[0],a[y[0]].vul); 41 for(int i=1;i<k2;++i){ 42 printf("%05d\n",y[i]); 43 printf("%05d %d ",y[i],a[y[i]].vul); 44 } 45 printf("-1\n"); 46 } 47 return 0; 48 }
1 #include<cstdio> 2 #include<algorithm> 3 #define N 1005 4 #define EPS 1e-8 5 using namespace std; 6 typedef pair<double,double> P; 7 P a[N+1]; 8 bool compare(P x,P y){ 9 return (x.second/x.first)>(y.second/y.first); 10 } 11 int main(void){ 12 int n; 13 double d; 14 //freopen("in.txt","r",stdin); 15 scanf("%d%lf",&n,&d); 16 for(int i=0;i<n;++i) 17 scanf("%lf",&a[i].first); 18 for(int i=0;i<n;++i) 19 scanf("%lf",&a[i].second); 20 sort(a,a+n,compare); 21 double temp=0,vul=0; 22 for(int i=0;temp<d&&i<n;++i){//考虑总和小于d的情况 23 if((temp+a[i].first<d)&&(temp+a[i].first-d)<EPS){ 24 temp+=a[i].first; 25 vul+=a[i].second; 26 }else{ 27 double t=d-temp; 28 temp=d; 29 vul+=(a[i].second*t/a[i].first); 30 } 31 } 32 printf("%.2lf\n",vul); 33 return 0; 34 }
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 using namespace std; 5 bool isST(int a[],int l,int r){ 6 if(l>=r)return 1; 7 int key=r; 8 for(int i=l+1;i<=r;++i){ 9 if(a[l]<=a[i]){ 10 key=i; 11 break; 12 } 13 } 14 for(int i=key+1;i<=r;++i) 15 if(a[l]>a[i])return 0; 16 return isST(a,l+1,key-1)&&isST(a,key,r); 17 } 18 bool isMirST(int a[],int l,int r){ 19 if(l>=r)return 1; 20 int key=r; 21 for(int i=l+1;i<=r;++i){ 22 if(a[l]>a[i]){ 23 key=i; 24 break; 25 } 26 } 27 for(int i=key+1;i<=r;++i) 28 if(a[l]<=a[i])return 0; 29 return isMirST(a,l+1,key-1)&&isMirST(a,key,r); 30 } 31 struct node{ 32 int vul; 33 node *l,*r; 34 }; 35 node *T=NULL; 36 node* st(node *t,int key){ 37 if(t==NULL){ 38 node *temp=(node*)malloc(sizeof(node)); 39 temp->vul=key; 40 temp->l=NULL; 41 temp->r=NULL; 42 return temp; 43 } 44 if(key>=t->vul)t->r=st(t->r,key); 45 else t->l=st(t->l,key); 46 return t; 47 } 48 node* Mirst(node *t,int key){ 49 if(t==NULL){ 50 node *temp=(node*)malloc(sizeof(node)); 51 temp->vul=key; 52 temp->l=NULL; 53 temp->r=NULL; 54 return temp; 55 } 56 if(key<t->vul)t->r=Mirst(t->r,key); 57 else t->l=Mirst(t->l,key); 58 return t; 59 } 60 void printT(node *t){ 61 if(t==NULL)return; 62 printT(t->l); 63 printT(t->r); 64 if(t!=T)printf("%d ",t->vul); 65 } 66 int main(void){ 67 freopen("in.txt","r",stdin); 68 int a[1001]; 69 memset(a,0,sizeof(a)); 70 int n; 71 scanf("%d",&n); 72 if(n==0){ 73 printf("YES\n"); 74 return 0; 75 } 76 for(int i=0;i<n;++i) 77 scanf("%d",&a[i]); 78 bool flag1=isST(a,0,n-1); 79 bool flag2=isMirST(a,0,n-1); 80 if(!(flag1||flag2)){ 81 printf("NO\n"); 82 return 0; 83 } 84 else if(flag1)for(int i=0;i<n;++i)T=st(T,a[i]); 85 else if(flag2)for(int i=0;i<n;++i)T=Mirst(T,a[i]); 86 printf("YES\n"); 87 printT(T); 88 printf("%d",T->vul); 89 printf("\n"); 90 return 0; 91 }
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 #include <set> 7 #include <algorithm> 8 #include <vector> 9 #include <map> 10 using namespace std; 11 typedef long long ll; 12 void debug(int x){ 13 printf("debug:%d\n",x); 14 } 15 int n,m,k[55],a[55][10005]; 16 int same(int u,int v){ 17 int j=0,cnt=0; 18 for(int i=0;i<k[u];++i){ 19 while(j<k[v]&&a[u][i]>a[v][j])j++; 20 if(a[u][i]==a[v][j])cnt++; 21 } 22 return cnt; 23 } 24 int main(void){ 25 scanf("%d",&n); 26 for(int i=0;i<n;++i){ 27 scanf("%d",&k[i]); 28 for(int j=0;j<k[i];++j) 29 scanf("%d",&a[i][j]); 30 sort(a[i],a[i]+k[i]); 31 k[i]=unique(a[i],a[i]+k[i])-a[i]; 32 } 33 scanf("%d",&m); 34 while(m--){ 35 int u,v; 36 scanf("%d%d",&u,&v); 37 int c=same(u-1,v-1); 38 printf("%.2lf%%\n",100.0*c/(k[u-1]+k[v-1]-c)); 39 } 40 }
1 #include<cstdio> 2 #include<cstdlib> 3 #include<queue> 4 #include<cstring> 5 #define N 30 6 using namespace std; 7 struct node{ 8 int vul; 9 node *r,*l; 10 }; 11 queue<node*>q; 12 node* creat(int a[],int al,int ar,int b[],int bl,int br){ 13 if(al>ar||bl>br)return 0; 14 int i,k=0; 15 for(i=bl;i<=br;++i){ 16 if(b[i]==a[ar])break; 17 k++; 18 } 19 node *t=(node*)malloc(sizeof(node)); 20 t->vul=a[ar]; 21 t->l=creat(a,al,al+k-1,b,bl,i-1); 22 t->r=creat(a,al+k,ar-1,b,i+1,br); 23 return t; 24 } 25 int main(void){ 26 //freopen("in.txt","r",stdin); 27 int a[N+1]; 28 int b[N+1]; 29 memset(a,0,sizeof(a)); 30 memset(b,0,sizeof(b)); 31 int n; 32 scanf("%d",&n); 33 for(int i=0;i<n;++i) 34 scanf("%d",&a[i]); 35 for(int i=0;i<n;++i) 36 scanf("%d",&b[i]); 37 node *t=creat(a,0,n-1,b,0,n-1); 38 node *temp; 39 printf("%d",t->vul); 40 q.push(t); 41 while(!q.empty()){ 42 temp=q.front(); 43 q.pop(); 44 if(temp){ 45 if(temp!=t)printf(" %d",temp->vul); 46 q.push(temp->l); 47 q.push(temp->r); 48 } 49 } 50 printf("\n"); 51 return 0; 52 }
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cmath> 5 #define EPS 1e-8 6 #define N 10000 7 using namespace std; 8 struct node{ 9 int house,area; 10 }; 11 struct res{ 12 int num; 13 int people; 14 int house; 15 int area; 16 double rh,ra; 17 }; 18 res r[1000]; 19 int k; 20 node a[N]; 21 int pre[N]; 22 bool mark[N]; 23 void Make(){ 24 for(int i=0;i<N;++i)pre[i]=i; 25 } 26 int Find(int x){ 27 if(x!=pre[x]) 28 pre[x]=Find(pre[x]); 29 return pre[x]; 30 } 31 void uion(int x,int y){ 32 int k1=Find(x),k2=Find(y); 33 k1<k2?pre[k2]=k1:pre[k1]=k2; 34 } 35 bool compare(res x,res y){ 36 if(fabs(x.ra-y.ra)<EPS)return x.num<y.num; 37 else return x.ra>y.ra; 38 } 39 int main(void){ 40 freopen("in.txt","r",stdin); 41 int n; 42 scanf("%d",&n); 43 Make(); 44 for(int i=0;i<n;++i){ 45 int me,father,mother,num,kid[5],house,area; 46 scanf("%d%d%d%d",&me,&father,&mother,&num); 47 mark[me]=1; 48 if(father!=-1){ 49 uion(me,father); 50 mark[father]=1; 51 } 52 if(mother!=-1){ 53 uion(me,mother); 54 mark[mother]=1; 55 } 56 for(int i=0;i<num;++i){ 57 scanf("%d",&kid[i]); 58 mark[kid[i]]=1; 59 uion(me,kid[i]); 60 } 61 scanf("%d%d",&house,&area); 62 a[me].house=house; 63 a[me].area=area; 64 } 65 for(int i=0;i<N;++i) 66 Find(i); 67 int p; 68 for(int i=0;i<N;++i){ 69 if(mark[i]){ 70 if(k==0){ 71 r[k].num=pre[i]; 72 r[k].people++; 73 r[k].house=a[i].house; 74 r[k].area=a[i].area; 75 k++; 76 }else{ 77 bool flag=true; 78 for(p=0;p<k;++p){ 79 if(r[p].num==pre[i]){ 80 flag=false; 81 break; 82 } 83 } 84 if(flag){ 85 r[k].num=pre[i]; 86 r[k].people++; 87 r[k].house=a[i].house; 88 r[k].area=a[i].area; 89 k++; 90 }else{ 91 r[p].people++; 92 r[p].house+=a[i].house; 93 r[p].area+=a[i].area; 94 } 95 } 96 } 97 } 98 for(int i=0;i<k;++i){ 99 r[i].rh=r[i].house*1.0/r[i].people; 100 r[i].ra=r[i].area*1.0/r[i].people; 101 } 102 printf("%d\n",k); 103 sort(r,r+k,compare); 104 for(int i=0;i<k;++i) 105 printf("%04d %d %.3lf %.3lf\n",r[i].num,r[i].people,r[i].rh,r[i].ra); 106 return 0; 107 }
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 char s[1001]; 6 int a[1001]; 7 int b[1001]; 8 int main(void){ 9 freopen("in.txt","r",stdin); 10 gets(s); 11 int len=strlen(s); 12 for(int i=0;i<len-1;++i){ 13 for(int j=0;;++j){ 14 if(i+j>=len||i-j<0)break; 15 if(s[i-j]==s[i+j])a[i]++; 16 else break; 17 } 18 if(s[i]==s[i+1]){ 19 for(int j=0;;++j){ 20 if(i+1+j>=len||i-j<0)break; 21 if(s[i-j]==s[i+1+j])b[i]++; 22 else break; 23 } 24 } 25 } 26 int Max=1; 27 for(int i=0;i<len;++i){ 28 Max=max(Max,a[i]*2-1); 29 Max=max(Max,b[i]*2); 30 } 31 printf("%d\n",Max); 32 return 0; 33 }
1 #include<cstdio> 2 using namespace std; 3 bool enemy[105][105]; 4 int pre[105]; 5 int Find(int a){ 6 if(a!=pre[a])pre[a]=Find(pre[a]); 7 return pre[a]; 8 } 9 void uion(int a,int b){ 10 int t1=Find(a),t2=Find(b); 11 if(t1!=t2)pre[t1]=t2; 12 } 13 void make(int n){ 14 for(int i=1;i<=n;i++)pre[i]=i; 15 } 16 int main(void){ 17 int n,m,k; 18 scanf("%d%d%d",&n,&m,&k); 19 make(n); 20 while(m--){ 21 int a,b,g; 22 scanf("%d%d%d",&a,&b,&g); 23 if(g==1)uion(a,b); 24 else enemy[a][b]=enemy[b][a]=1; //a和b是敌人 25 } 26 while(k--){ 27 int a,b; 28 scanf("%d%d",&a,&b); 29 int ta=Find(a),tb=Find(b); 30 if(enemy[a][b]){ 31 if(ta==tb)printf("OK but...\n"); 32 else printf("No way\n"); 33 }else{ 34 if(ta==tb)printf("No problem\n"); 35 else printf("OK\n"); 36 } 37 } 38 return 0; 39 }
1 #include<cstdio> 2 using namespace std; 3 bool enemy[105][105]; 4 int pre[105]; 5 int Find(int a){ 6 if(a!=pre[a])pre[a]=Find(pre[a]); 7 return pre[a]; 8 } 9 void uion(int a,int b){ 10 int t1=Find(a),t2=Find(b); 11 if(t1!=t2)pre[t1]=t2; 12 } 13 void make(int n){ 14 for(int i=1;i<=n;i++)pre[i]=i; 15 } 16 int main(void){ 17 int n,m,k; 18 scanf("%d%d%d",&n,&m,&k); 19 make(n); 20 while(m--){ 21 int a,b,g; 22 scanf("%d%d%d",&a,&b,&g); 23 if(g==1)uion(a,b); 24 else enemy[a][b]=enemy[b][a]=1; 25 } 26 while(k--){ 27 int a,b; 28 scanf("%d%d",&a,&b); 29 int ta=Find(a),tb=Find(b); 30 if(enemy[a][b]){//a和b是敌人 31 if(ta==tb)printf("OK but...\n"); 32 else printf("No way\n"); 33 }else{ 34 if(ta==tb)printf("No problem\n"); 35 else printf("OK\n"); 36 } 37 } 38 return 0; 39 }
1 #include<cstdio> 2 #include<cstdlib> 3 #include<queue> 4 #define N 30 5 using namespace std; 6 typedef struct node{ 7 int v; 8 node *l,*r; 9 }*Node; 10 int mid[N];//中序 11 int pre[N];//前序 12 int res[N];//结果 13 int n; 14 int Find(int x){//在中序数组中找树根的位置 15 for(int i=0;i<n;++i){ 16 if(mid[i]==x) 17 return i; 18 } 19 return -1; 20 } 21 Node Make(int pl,int pr,int ml,int mr){//构造镜像树 22 if(pl>pr)return NULL; 23 Node T=(Node)malloc(sizeof(node)); 24 T->v=pre[pl]; 25 int t=Find(pre[pl]); 26 T->r=Make(pl+1,pl+t-ml,ml,t-1); 27 T->l=Make(pl+t-ml+1,pr,t+1,mr); 28 return T; 29 } 30 int main(void){ 31 scanf("%d",&n); 32 for(int i=0;i<n;++i){ 33 scanf("%d",mid+i); 34 } 35 for(int i=0;i<n;++i){ 36 scanf("%d",pre+i); 37 } 38 Node T=Make(0,n-1,0,n-1); 39 40 queue<Node>q; 41 q.push(T); 42 int k=0; 43 while(!q.empty()){ 44 Node t=q.front(); 45 q.pop(); 46 if(t){ 47 res[k++]=t->v; 48 q.push(t->l); 49 q.push(t->r); 50 } 51 } 52 53 printf("%d",res[0]); 54 for(int i=1;i<k;++i){ 55 printf(" %d",res[i]); 56 } 57 printf("\n"); 58 return 0; 59 }
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 int a[1005]; 5 int location(int key){ 6 int k=0; 7 while(a[k]!=key)k++; 8 return k; 9 } 10 int transint(char s[]){ 11 if(s[0]=='-'){ 12 int temp=0; 13 for(int i=1;s[i]!=' '&&s[i]!='\0';i++) 14 temp=temp*10+s[i]-'0'; 15 return -temp; 16 }else{ 17 int temp=0; 18 for(int i=0;s[i]!=' '&&s[i]!='\0';i++) 19 temp=temp*10+s[i]-'0'; 20 return temp; 21 } 22 } 23 int main(void){ 24 int n,m; 25 scanf("%d%d",&n,&m); 26 for(int i=0;i<n;i++){ 27 scanf("%d",a+i); 28 int k=i; 29 while(k&&a[k]<a[(k-1)/2]){ 30 swap(a[k],a[(k-1)/2]); 31 k=(k-1)/2; 32 } 33 } 34 while(m--){ 35 bool flag; 36 char s[50]; 37 int x; 38 scanf("%d",&x); 39 gets(s);//由于gets会读取x后的分隔符,所以字符串s实际上是从' '开始的 40 //一开始用s[8] 41 if(s[4]=='t'&&s[8]=='r'){//第一种查询 42 if(a[0]==x)flag=1; 43 else flag=0; 44 }else if(s[4]==' '){//第二种查询 45 int y=transint(&s[5]); 46 int t=location(x); 47 if(t){ 48 if(t&1){ 49 if(a[t+1]==y)flag=1; 50 else flag=0; 51 }else{ 52 if(a[t-1]==y)flag=1; 53 else flag=0; 54 } 55 }else flag=0; 56 }else if(s[4]=='t'&&s[8]=='p'){//第三种查询 57 int y=transint(&s[18]); 58 int t=location(y); 59 if(t&&a[(t-1)/2]==x)flag=1; 60 else flag=0; 61 }else if(s[4]=='a'){//第四种查询 62 int y=transint(&s[15]); 63 int t=location(x); 64 if(t&&a[(t-1)/2]==y)flag=1; 65 else flag=0; 66 } 67 if(flag)printf("T\n"); 68 else printf("F\n"); 69 } 70 return 0; 71 }
1 #include <cstdio> 2 #include <vector> 3 #include <set> 4 #define N 505 5 using namespace std; 6 int n,m,k,pre[N],a[5005]; 7 vector<int>e[N]; 8 set<int>s,rest; 9 struct node{ 10 int alert,id; 11 }ans[5005]; 12 void debug(char *a,int x){ 13 printf("%s:%d\n",a,x); 14 } 15 int Find(int x){ 16 return x==pre[x]?x:pre[x]=Find(pre[x]); 17 } 18 void Union(int a,int b){ 19 int x=Find(a),y=Find(b); 20 if(x!=y)pre[x]=y; 21 } 22 int main(void){ 23 for(int i=0;i<=500;++i)pre[i]=i; 24 scanf("%d%d",&n,&m); 25 for(int i=0;i<m;++i){ 26 int u,v; 27 scanf("%d%d",&u,&v); 28 e[u].push_back(v); 29 e[v].push_back(u); 30 } 31 scanf("%d",&k); 32 for(int i=0;i<k;++i){ 33 scanf("%d",&a[i]); 34 rest.insert(a[i]); 35 } 36 for(int i=0;i<n;++i){ 37 if(rest.count(i)==0){ 38 for(int j=0;j<e[i].size();++j){ 39 int v=e[i][j]; 40 if(s.count(v)==1)Union(i,v); 41 } 42 s.insert(i); 43 } 44 } 45 for(int i=k-1;i>=0;--i){ 46 int x=a[i],pre=-1; 47 ans[i]=(node){0,x}; 48 for(int j=0;j<e[x].size();++j){ 49 int v=e[x][j]; 50 if(s.count(v)==1){ 51 if(pre==-1)pre=Find(v); 52 else if(pre!=Find(v))ans[i].alert=1; 53 Union(x,v); 54 } 55 } 56 s.insert(x); 57 } 58 for(int i=0;i<k;++i){ 59 if(ans[i].alert)printf("Red Alert: City %d is lost!\n",ans[i].id); 60 else printf("City %d is lost.\n",ans[i].id); 61 } 62 if(k==n)printf("Game Over.\n"); 63 }
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 int a[100005],k,n; 5 int main(void){ 6 cin>>n; 7 for(int i=0;i<n;++i){ 8 int x; 9 cin>>x; 10 int idx=lower_bound(a,a+k,x)-a; 11 if(idx==k)a[k++]=x; 12 else a[idx]=x; 13 } 14 cout<<k<<endl; 15 }
1 #include <cstdio> 2 #include <queue> 3 #include <iostream> 4 using namespace std; 5 int n,k,m; 6 double ans[25]; 7 priority_queue<double>q; 8 int main(void){ 9 cin>>n>>k>>m; 10 for(int i=0;i<n;++i){ 11 int maxn=-1,minn=101,sum=0; 12 for(int j=0;j<k;++j){ 13 int x; 14 cin>>x; 15 sum+=x; 16 maxn=max(maxn,x); 17 minn=min(minn,x); 18 } 19 q.push(1.0*(sum-minn-maxn)/(k-2)); 20 } 21 for(int i=0;i<m;++i){ 22 double t=q.top();q.pop(); 23 ans[i]=t; 24 } 25 for(int i=m-1;i>=0;--i){ 26 printf("%.3lf%c",ans[i],i==0?'\n':' '); 27 } 28 }
1 /* 2 本题坑点: 3 1.题目说的五代(本人、父母、祖父母、曾祖父母、高祖父母)包括外婆那部分. 4 2.查询时有可能查询在本人ID内没出现过的ID(父母的ID) 5 */ 6 #include <cstdio> 7 #include <set> 8 #define N 100005 9 using namespace std; 10 int n,k; 11 bool f; 12 set<int>s; 13 struct node{ 14 int fa,ma; 15 char sex; 16 node(int FA=-1,int MA=-1,char SEX=0){ 17 fa=FA;ma=MA;sex=SEX; 18 } 19 }p[N]; 20 void dfs_u(int x,int d){ 21 if(x==-1||d>5)return; 22 s.insert(x); 23 dfs_u(p[x].fa,d+1); 24 dfs_u(p[x].ma,d+1); 25 } 26 void dfs_v(int x,int d){ 27 if(!f)return; 28 if(x==-1||d>5)return; 29 if(s.count(x))f=0; 30 dfs_v(p[x].fa,d+1); 31 dfs_v(p[x].ma,d+1); 32 } 33 int main(void){ 34 scanf("%d",&n); 35 for(int i=0;i<n;++i){ 36 int id,fa,ma; 37 char sex; 38 scanf("%d%*c%c%*c%d%d",&id,&sex,&fa,&ma); 39 p[id]=node(fa,ma,sex); 40 if(p[fa].sex==0)p[fa]=node(-1,-1,'M'); 41 if(p[ma].sex==0)p[ma]=node(-1,-1,'F'); 42 } 43 scanf("%d",&k); 44 for(int i=0;i<k;++i){ 45 int u,v; 46 scanf("%d%d",&u,&v); 47 if(p[u].sex==p[v].sex){ 48 printf("Never Mind\n"); 49 continue; 50 } 51 s.clear();f=1; 52 dfs_u(u,1); 53 dfs_v(v,1); 54 if(f)puts("Yes"); 55 else puts("No"); 56 } 57 }
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<stack> 5 using namespace std; 6 const int INF=-0x3fffffff; 7 stack<int>s; 8 int dp[101],pre[101],a[10001]; 9 int main(void){ 10 //freopen("in.txt","r",stdin); 11 for(int i=0;i<=100;++i){ 12 dp[i]=INF; 13 pre[i]=-1; 14 } 15 dp[0]=0; 16 int n,m; 17 scanf("%d%d",&n,&m); 18 for(int i=1;i<=n;++i) 19 scanf("%d",&a[i]); 20 sort(&a[1],&a[1]+n); 21 for(int i=1;i<=n;++i){ 22 for(int j=m;j>=a[i];--j){ 23 if(dp[j]<=dp[j-a[i]]+1){ 24 dp[j]=dp[j-a[i]]+1; 25 pre[j]=j-a[i]; 26 } 27 } 28 } 29 if(dp[m]>0){ 30 int k=m; 31 while(pre[k]){ 32 s.push(k-pre[k]); 33 k=pre[k]; 34 } 35 printf("%d",k); 36 while(!s.empty()){ 37 printf(" %d",s.top()); 38 s.pop(); 39 } 40 printf("\n"); 41 }else printf("No Solution\n"); 42 return 0; 43 }
1 #include<cstdio> 2 #include<algorithm> 3 #include<stack> 4 #define N 100001 5 #define lson l,m,n<<1 6 #define rson m+1,r,n<<1|1 7 using namespace std; 8 stack<int>pq; 9 int st[N<<2]; 10 void build(int l,int r,int n,int key); 11 void change(int l,int r,int n,int key); 12 void updata(int n); 13 int query(int l,int r,int n,int key); 14 int main(void){ 15 freopen("in.txt","r",stdin); 16 int n,key; 17 char s[15]; 18 scanf("%d",&n); 19 while(n--){ 20 scanf("%s",s); 21 switch(s[1]){ 22 case 'o': 23 if(pq.empty())printf("Invalid\n"); 24 else{ 25 key=pq.top(); 26 printf("%d\n",key); 27 pq.pop(); 28 change(1,100000,1,key); 29 } 30 break; 31 case 'u': 32 scanf("%d",&key); 33 pq.push(key); 34 build(1,100000,1,key); 35 break; 36 case 'e': 37 if(pq.empty())printf("Invalid\n"); 38 else{ 39 key=pq.size()+1; 40 key>>=1; 41 printf("%d\n",query(1,100000,1,key)); 42 } 43 break; 44 } 45 } 46 return 0; 47 } 48 void build(int l,int r,int n,int key){ 49 if(l>=r){ 50 st[n]++; 51 return; 52 } 53 int m=(l+r)/2; 54 if(key<=m)build(lson,key); 55 else build(rson,key); 56 updata(n); 57 } 58 void change(int l,int r,int n,int key){ 59 if(l==r){ 60 st[n]--; 61 return; 62 } 63 int m=(l+r)/2; 64 if(key<=m)change(lson,key); 65 else change(rson,key); 66 updata(n); 67 } 68 void updata(int n){ 69 st[n]=st[n<<1]+st[n<<1|1]; 70 } 71 int query(int l,int r,int n,int key){ 72 if(l==r)return l; 73 int m=(l+r)/2; 74 if(key<=st[n<<1])return query(lson,key); 75 else return query(rson,key-st[n<<1]); 76 }
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #define N 1005 5 using namespace std; 6 int a[N];//num 7 int pre[N+1]; 8 int r[N];//sum 9 void Make(); 10 int Find(int n); 11 void Uion(int x,int y); 12 bool compare(int x,int y){ 13 return x>y; 14 } 15 int main(void){ 16 freopen("in.txt","r",stdin); 17 int n; 18 Make(); 19 scanf("%d",&n); 20 for(int i=0;i<n;++i){ 21 int t,h,Min,p; 22 scanf("%d:",&t); 23 for(int j=0;j<t;++j){ 24 scanf("%d",&h); 25 if(!j){ 26 Min=h; 27 p=h; 28 }else{ 29 Min=min(Min,h); 30 Uion(h,p); 31 } 32 } 33 a[i]=Min; 34 } 35 for(int i=1;i<=N;++i)Find(i); 36 for(int i=0;i<n;++i) 37 r[pre[a[i]]]++; 38 sort(r,r+N,compare); 39 int k; 40 for(k=0;k<N;k++) 41 if(!r[k])break; 42 printf("%d\n",k); 43 printf("%d",r[0]); 44 for(int i=1;i<k;++i) 45 printf(" %d",r[i]); 46 printf("\n"); 47 return 0; 48 } 49 void Make(){ 50 for(int i=0;i<=N;++i) 51 pre[i]=i; 52 } 53 int Find(int n){ 54 if(n!=pre[n])pre[n]=Find(pre[n]); 55 return pre[n]; 56 } 57 void Uion(int x,int y){ 58 int k1=Find(x),k2=Find(y); 59 k1<k2?pre[k2]=k1:pre[k1]=k2; 60 }
1 /**dfs over_stack**/ 2 #include<cstdio> 3 #include<iostream> 4 #include<queue> 5 using namespace std; 6 struct position{ 7 int x,y,z; 8 }; 9 int a[65][1291][133]; 10 int dx[6]={-1,1,0,0,0,0}; 11 int dy[6]={0,0,0,0,1,-1}; 12 int dz[6]={0,0,-1,1,0,0}; 13 int l,m,n; 14 queue<position>q; 15 int bfs(int x,int y,int z); 16 int main(void){ 17 freopen("in.txt","r",stdin); 18 int t,sum=0,temp; 19 scanf("%d%d%d%d",&m,&n,&l,&t); 20 for(int i=0;i<l;i++) 21 for(int j=0;j<m;++j) 22 for(int k=0;k<n;++k) 23 scanf("%d",&a[i][j][k]); 24 for(int i=0;i<l;i++){ 25 for(int j=0;j<m;j++){ 26 for(int k=0;k<n;k++){ 27 if(a[i][j][k]){ 28 temp=bfs(i,j,k); 29 if(temp>=t)sum+=temp; 30 } 31 } 32 } 33 } 34 printf("%d\n",sum); 35 return 0; 36 } 37 int bfs(int x,int y,int z){ 38 position t,tt; 39 t.x=x,t.y=y,t.z=z; 40 int temp=0; 41 q.push(t); 42 while(!q.empty()){ 43 t=q.front(); 44 q.pop(); 45 if(a[t.x][t.y][t.z]){ 46 temp++; 47 a[t.x][t.y][t.z]=0; 48 for(int i=0;i<6;++i){ 49 tt.x=t.x+dx[i]; 50 tt.y=t.y+dy[i]; 51 tt.z=t.z+dz[i]; 52 if((0<=tt.x&&tt.x<l)&&(0<=tt.y&&tt.y<m)&&(0<=tt.z&&tt.z<n)) 53 q.push(tt); 54 } 55 } 56 } 57 return temp; 58 }
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #define N 1000 6 #define M 10 7 #define EPS 1e-8 8 using namespace std; 9 const int INF=0x3fffffff; 10 int mp[N+M+1][N+M+1]; 11 int n,m,d; 12 int Dis[M+1][N+M+1]; 13 bool out[N+M+1]; 14 struct res{ 15 double ave,mindis; 16 int num; 17 bool mark; 18 }; 19 res r[11]; 20 int inti(char a[]); 21 void dijstra(int x); 22 bool compare(res a,res b){ 23 if(a.mindis!=b.mindis) 24 return a.mindis>b.mindis; 25 else{ 26 if(fabs(a.ave-b.ave)<EPS) 27 return a.num<b.num; 28 else 29 return a.ave<b.ave; 30 } 31 } 32 int main(void){ 33 //freopen("in.txt","r",stdin); 34 int k; 35 for(int i=1;i<=N+M;i++) 36 for(int j=1;j<=N+M;j++) 37 mp[i][j]=INF; 38 scanf("%d%d%d%d",&n,&m,&k,&d); 39 while(k--){ 40 char a[2][5]; 41 int b[2],dis; 42 scanf("%s%s%d\n",a[0],a[1],&dis); 43 for(int i=0;i<2;i++){ 44 if('1'<=a[i][0]&&a[i][0]<='9') 45 b[i]=inti(a[i]); 46 else 47 b[i]=N+inti(&a[i][1]); 48 } 49 mp[b[0]][b[1]]=mp[b[1]][b[0]]=dis; 50 } 51 for(int i=1;i<=m;i++){ 52 for(int j=1;j<=N+m;j++) 53 Dis[i][j]=INF; 54 memset(out,1,sizeof(out)); 55 dijstra(i); 56 } 57 for(int i=1;i<=m;i++){ 58 r[i].mindis=INF; 59 r[i].num=i; 60 for(int j=1;j<=n;j++){ 61 if(Dis[i][j]!=INF){ 62 if(Dis[i][j]>d) 63 r[i].mark=1; 64 if(r[i].mindis>Dis[i][j]) 65 r[i].mindis=Dis[i][j]; 66 r[i].ave+=Dis[i][j]; 67 } 68 } 69 r[i].ave/=n; 70 } 71 sort(&r[1],&r[1]+m,compare); 72 bool flag=1; 73 for(int i=1;i<=m;i++){ 74 if(r[i].mark)continue; 75 printf("G%d\n",r[i].num); 76 printf("%.1lf %.1lf\n",r[i].mindis,r[i].ave); 77 flag=0; 78 break; 79 } 80 if(flag) 81 printf("No Solution\n"); 82 return 0; 83 } 84 int inti(char a[]){ 85 int len=strlen(a); 86 int temp=0; 87 for(int i=0;i<len;i++) 88 temp=temp*10+(a[i]-'0'); 89 return temp; 90 } 91 void dijstra(int x){ 92 Dis[x][N+x]=0; 93 while(1){ 94 int Min=INF,index; 95 for(int i=1;i<=N+m;i++){ 96 if(out[i]&&Min>Dis[x][i]){ 97 Min=Dis[x][i]; 98 index=i; 99 } 100 } 101 if(Min==INF)break; 102 out[index]=0; 103 for(int i=1;i<=N+m;++i){ 104 if(out[i]&&Dis[x][index]+mp[index][i]<Dis[x][i]) 105 Dis[x][i]=Dis[x][index]+mp[index][i]; 106 } 107 } 108 }
1 #include<cstdio> 2 #include<cstring> 3 #include<stack> 4 #define N 505 5 using namespace std; 6 const int INF=0x3f3f3f3f; 7 bool mark[N]; 8 int n,Start,End; 9 int lenmp[N][N]; 10 int timemp[N][N]; 11 int lendis[N]; 12 int timedis[N]; 13 int lenpre[N]; 14 int timepre[N]; 15 void lenDijstra(); 16 void timeDijstra(); 17 int main(void){ 18 /**输入**/ 19 memset(lenpre,-1,sizeof(lenpre)); 20 memset(timepre,-1,sizeof(timepre)); 21 int m; 22 scanf("%d%d",&n,&m); 23 for(int i=0;i<n;++i){ 24 lendis[i]=INF; 25 timedis[i]=INF; 26 for(int j=0;j<n;++j){ 27 lenmp[i][j]=INF; 28 timemp[i][j]=INF; 29 } 30 } 31 while(m--){ 32 int a,b,one,length,time; 33 scanf("%d%d%d%d%d",&a,&b,&one,&length,&time); 34 if(one){ 35 lenmp[a][b]=length; 36 timemp[a][b]=time; 37 }else{ 38 lenmp[a][b]=lenmp[b][a]=length; 39 timemp[a][b]=timemp[b][a]=time; 40 } 41 } 42 scanf("%d%d",&Start,&End); 43 /**数据处理**/ 44 lenDijstra(); 45 timeDijstra(); 46 /**输出**/ 47 int k=End; 48 bool flag=1; 49 stack<int>times; 50 while(k!=Start){ 51 times.push(k); 52 if(timepre[k]!=lenpre[k])flag=0; 53 k=timepre[k]; 54 } 55 if(flag){ 56 printf("Time = %d;",timedis[End]); 57 printf(" Distance = %d:",lendis[End]); 58 printf("% d",Start); 59 while(!times.empty()){ 60 printf(" => %d",times.top()); 61 times.pop(); 62 } 63 printf("\n"); 64 }else{ 65 printf("Time = %d:",timedis[End]); 66 printf("% d",Start); 67 while(!times.empty()){ 68 printf(" => %d",times.top()); 69 times.pop(); 70 } 71 printf("\n"); 72 printf("Distance = %d:",lendis[End]); 73 printf("% d",Start); 74 stack<int>diss; 75 k=End; 76 while(k!=Start){ 77 diss.push(k); 78 k=lenpre[k]; 79 } 80 while(!diss.empty()){ 81 printf(" => %d",diss.top()); 82 diss.pop(); 83 } 84 printf("\n"); 85 } 86 return 0; 87 } 88 void lenDijstra(){ 89 int node[N]; 90 memset(node,0,sizeof(node)); 91 node[Start]=1; 92 93 memset(mark,1,sizeof(mark)); 94 lendis[Start]=0; 95 while(1){ 96 int Min=INF,index; 97 for(int i=0;i<n;++i){ 98 if(mark[i]&&Min>lendis[i]){ 99 Min=lendis[i]; 100 index=i; 101 } 102 } 103 if(Min==INF)break; 104 mark[index]=0; 105 for(int i=0;i<n;++i){ 106 int d=lendis[index]+lenmp[index][i]; 107 if(mark[i]&&lendis[i]>d){ 108 lendis[i]=d; 109 lenpre[i]=index; 110 node[i]=node[index]+1;/*非node[i]++*/ 111 }else if(mark[i]&&lendis[i]==d&&node[index]+1<node[i]){ 112 lenpre[i]=index; 113 node[i]=node[index]+1; 114 } 115 } 116 } 117 } 118 void timeDijstra(){ 119 memset(mark,1,sizeof(mark)); 120 int dis[N]; 121 for(int i=0;i<n;++i) 122 dis[i]=lenmp[Start][i]; 123 timedis[Start]=0; 124 dis[Start]=0; 125 while(1){ 126 int Min=INF,index; 127 for(int i=0;i<n;++i){ 128 if(mark[i]&&Min>timedis[i]){ 129 Min=timedis[i]; 130 index=i; 131 } 132 } 133 if(Min==INF)break; 134 mark[index]=0; 135 for(int i=0;i<n;++i){ 136 int t=timedis[index]+timemp[index][i]; 137 int d=dis[index]+lenmp[index][i]; 138 if(mark[i]&&timedis[i]>t){ 139 dis[i]=d; 140 timedis[i]=t; 141 timepre[i]=index; 142 }else if(mark[i]&&timedis[i]==t&&dis[i]>d){ 143 dis[i]=d; 144 timepre[i]=index; 145 } 146 } 147 } 148 }
1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <queue> 5 #define X first 6 #define Y second 7 #define N 10005 8 using namespace std; 9 int n,m,k; 10 bool vis[N]; 11 vector<int>e[N]; 12 struct node{ 13 int id,d; 14 node(int ID=-1,int D=-1){id=ID;d=D;} 15 bool operator<(node t)const{ 16 if(d==t.d)return id>t.id; 17 return d<t.d; 18 } 19 }; 20 void debug(string s,int v){ 21 cout<<s<<":"<<v<<"\n"; 22 } 23 int bfs(int x){ 24 memset(vis,0,sizeof(vis)); 25 node ans=node(x,0); 26 queue<node>q; 27 q.push(ans); 28 while(!q.empty()){ 29 node t=q.front();q.pop(); 30 if(!vis[t.id]){ 31 vis[t.id]=1; 32 ans=max(ans,t); 33 for(int i=0;i<(int)e[t.id].size();++i) 34 q.push(node(e[t.id][i],t.d+1)); 35 } 36 } 37 return ans.d?ans.id:0; 38 } 39 int main(void){ 40 std::ios::sync_with_stdio(false); 41 cin>>n>>m>>k; 42 for(int i=1;i<=m;++i){ 43 int u,v; 44 cin>>u>>v; 45 e[u].push_back(v); 46 e[v].push_back(u); 47 } 48 for(int i=1;i<=k;++i){ 49 int x;cin>>x; 50 cout<<bfs(x)<<"\n"; 51 } 52 }
1 #include <iostream> 2 #include <cmath> 3 #include <queue> 4 using namespace std; 5 int n,k,dep,ans[50],d_; 6 struct node{ 7 int x,l,r; 8 node(int X=-1,int L=-1,int R=-1){ 9 x=X;l=L;r=R; 10 } 11 }a[50]; 12 void cha(int &rt,int x,int d){ 13 if(rt==-1){ 14 rt=k; 15 if(d==dep-1)d_++; 16 a[k++].x=x; 17 return; 18 } 19 if(a[rt].x<x)cha(a[rt].l,x,d+1); 20 else cha(a[rt].r,x,d+1); 21 } 22 int main(void){ 23 cin>>n; 24 for(int i=1;i<=5;++i){ 25 if((1<<i)>=n+1){ 26 dep=i; 27 break; 28 } 29 } 30 int root=-1; 31 for(int i=0;i<n;++i){ 32 int x; 33 cin>>x; 34 cha(root,x,1); 35 } 36 queue<int>q; 37 q.push(0); 38 int k=0; 39 bool bk=1,complete=1; 40 while(!q.empty()){ 41 int t=q.front();q.pop(); 42 ans[k++]=a[t].x; 43 if(a[t].l!=-1){ 44 q.push(a[t].l); 45 if(!bk)complete=0; 46 }else bk=0; 47 if(a[t].r!=-1){ 48 q.push(a[t].r); 49 if(!bk)complete=0; 50 }else bk=0; 51 } 52 for(int i=0;i<k-1;++i) 53 cout<<ans[i]<<" "; 54 cout<<ans[k-1]<<"\n"; 55 if(n==1||(complete&&d_==(1<<(dep-2))))cout<<"YES\n"; 56 else cout<<"NO\n"; 57 }
1 /* 2 本题坑点: 3 1.最快进攻路径的条数指的并非是最合适进攻路径的边数,而是距离最短的路径有多少条 4 */ 5 #include <iostream> 6 #include <string> 7 #include <map> 8 #define N 205 9 using namespace std; 10 const int inf=0x03ffffff; 11 int n,k,mp[N][N],s,t,idx,def[N]; 12 int pre[N],dis[N],ps[N],num[N],kill[N],vis[N],rount[N]; 13 string i2s[N]; 14 map<string,int>s2i; 15 void debug(string s,int v){ 16 cout<<s<<":"<<v<<"\n"; 17 } 18 void init(){ 19 string ss,st; 20 cin>>n>>k>>ss>>st; 21 for(int i=0;i<n;++i){ 22 pre[i]=-1;dis[i]=inf; 23 for(int j=0;j<n;++j){ 24 mp[i][j]=inf; 25 } 26 } 27 for(int i=1;i<n;++i){ 28 string s;int df; 29 cin>>s>>df; 30 def[idx]=df; 31 i2s[idx]=s; 32 s2i[s]=idx++; 33 } 34 s=s2i[ss]=idx; 35 i2s[idx]=ss; 36 t=s2i[st]; 37 for(int i=0;i<k;++i){ 38 string su,sv; 39 int d; 40 cin>>su>>sv>>d; 41 int u=s2i[su],v=s2i[sv]; 42 mp[u][v]=min(mp[u][v],d); 43 mp[v][u]=min(mp[v][u],d); 44 } 45 dis[s]=0;num[s]=1;rount[s]=1; 46 } 47 void dfs_printf(int x){ 48 if(x==-1)return; 49 dfs_printf(pre[x]); 50 cout<<i2s[x]<<"->"; 51 } 52 int main(void){ 53 std::ios::sync_with_stdio(false); 54 init(); 55 while(1){ 56 int k,maxn=inf; 57 for(int i=0;i<n;++i) 58 if(!vis[i]&&dis[i]<maxn) 59 maxn=dis[k=i]; 60 if(maxn==inf)break; 61 vis[k]=1; 62 for(int i=0;i<n;++i){ 63 int d=dis[k]+mp[k][i]; 64 int m=num[k]+1; 65 int p=ps[k]+def[i]; 66 if(dis[i]>d){ 67 pre[i]=k; 68 dis[i]=d; 69 num[i]=m; 70 ps[i]=p; 71 rount[i]=rount[k]; 72 }else if(dis[i]==d){ 73 rount[i]+=rount[k]; 74 if(num[i]<m){ 75 pre[i]=k; 76 num[i]=m; 77 ps[i]=p; 78 }else if(num[i]==m){ 79 if(ps[i]<p){ 80 pre[i]=k; 81 ps[i]=p; 82 } 83 } 84 } 85 } 86 } 87 dfs_printf(pre[t]); 88 cout<<i2s[t]<<"\n"<<rount[t]<<" "<<dis[t]<<" "<<ps[t]<<"\n"; 89 }