• PAT甲级题解分类byZlc


    专题一  字符串处理

    A1001 Format(20)  

    #include<cstdio>
    int main () {
        int s[11];
        int a,b,sum;
        scanf ("%d %d",&a,&b);
        sum=a+b;
        if (sum<0) {
            printf ("-");
            sum=-sum;
        }
        int z,top=0;
        if (sum==0) {
            s[top++]=0;
        }
        while (sum!=0) {
            
            s[top++]=sum%10;
            sum/=10;
        }
        
        for (int i=top-1;i>=0;i--) {
            printf ("%d",s[i]);
            
            if (i%3==0&&i!=0) printf (",");
        }
        return 0;
    } 
    View Code

    A1005 Spell It Right(20)

    #include<cstdio>
    int main () {
        char s[10][6]={"zero","one","two","three","four","five","six","seven","eight","nine"};
        char q[105];
        int w=0;
        while ((q[w]=getchar())!='
    ')
        w++;
        q[w]='';
        int a[105],sum=0;
        for (int i=0;i<w;i++)
        a[i]=q[i]-'0';
        for (int i=0;i<w;i++)
        sum+=a[i];
        int z[10],top;
        while (sum!=0) {
            z[top++]=sum%10;
            sum/=10;
        }
        printf ("%s",s[z[top-1]]);
        for (int i=top-2;i>=0;i--) {
            printf (" %s",s[z[i]]);
        }
        return 0;
    } 
    View Code

    A1023 Have Fun with Numbers(20)

    #include<cstdio>
    #include<cstring>
    using namespace std;
    int book[10];
    int main () {
        char num[22];
        scanf ("%s",num);
        int flag=0,len=strlen(num);
        for (int i=len-1;i>=0;i--) {
            int temp=num[i]-'0';
            book[temp]++;
            temp=temp*2+flag;
            flag=0;
            if (temp>=10) {
                temp=temp-10;
                flag=1;
            }
            num[i]=temp+'0';
            book[temp]--;
        }
        int flag1=0;
        for (int i=0;i<10;i++)
        if (book[i]!=0) flag1=1;
        printf ("%s",(flag==1||flag1==1)?"No
    ":"Yes
    ");
        if (flag==1) printf ("1");
        printf ("%s",num);
        return 0;
    }
    View Code

    A1077 Kuchiguse(20)

    #include<cstdio>
    #include<cstring>
    int n,minLen=256,ans=0;
    char s[100][256];
    int main () {
        scanf ("%d",&n);
        getchar ();
        for (int i=0;i<n;i++) {
            int w=0;
            while ((s[i][w]=getchar())!='
    ')
            w++;
            s[i][w]='';
            int len=strlen(s[i]);
            if (len<minLen) minLen=len;
            for (int j=0;j<len/2;j++) {
                char temp=s[i][j];
                s[i][j]=s[i][len-j-1];
                s[i][len-j-1]=temp;
            }
        }
        for (int i=0;i<minLen;i++) {
            char c=s[0][i];
            bool same=true;
            for (int j=1;j<n;j++) {
                if (c!=s[j][i]) {
                    same=false;
                    break;
                }
            }
            if (same) ans++;
            else break;
        }
        if (ans) {
            for (int i=ans-1;i>=0;i--) 
            printf ("%c",s[0][i]);
        }
        else printf ("nai");
        return 0;
    }
    View Code

    专题二  树的遍历

    A1004 Counting Leaves(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    vector<int> g[maxn];
    int d[maxn];
    int maxdepth=-1;
    void dfs (int root,int depth) {
        maxdepth=max(maxdepth,depth);
        if (g[root].size()==0) d[depth]++;
        for (int i=0;i<g[root].size();i++)
        dfs (g[root][i],depth+1);
    } 
    int main () {
        int N,M;
        scanf ("%d %d",&N,&M);
        int num,k,x;
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&num,&k);
            for (int j=0;j<k;j++) {
                scanf ("%d",&x);
                g[num].push_back(x);
            }
        }
        dfs (1,0);
        for (int i=0;i<=maxdepth;i++) {
            if (i!=0) printf (" ");
            printf ("%d",d[i]);
        }
        return 0;
    }
    View Code

    A1053 Path of Equal Weight(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    struct node {
        int data;
        vector<int> child;
    }Node[maxn];
    vector<int> path;
    int N,M,S;
    bool cmp (int a,int b) {
        return Node[a].data>Node[b].data;
    } 
    void dfs (int root,int sum) {
        path.push_back(root);
        if (Node[root].child.size()==0) {
            if (sum==S) {
                for (int i=0;i<path.size();i++) {
                    if (i!=0) printf (" ");
                    printf ("%d",Node[path[i]].data);
                }
                printf ("
    ");
            }
            path.pop_back();
            return;
        }
        for (int i=0;i<Node[root].child.size();i++)
        dfs (Node[root].child[i],sum+Node[Node[root].child[i]].data);
        path.pop_back();
        return;
    }
    int main () {
        scanf ("%d %d %d",&N,&M,&S);
        for (int i=0;i<N;i++) {
            scanf ("%d",&Node[i].data);
        }
        int num,k,x;
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&num,&k);
            for (int j=0;j<k;j++) {
                scanf ("%d",&x);
                Node[num].child.push_back(x);
            }
            sort (Node[num].child.begin(),Node[num].child.end(),cmp);
        }
        dfs (0,Node[0].data);
        return 0;
    }
    View Code

    A1079 Total Sales of Supply Chain(25)

    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int maxn=100005;
    int n;
    double p,r;
    struct node {
        int data;
        vector<int> child;
    }Node[maxn];
    double sum=0;
    void DFS (int root,int depth) {
        if (Node[root].child.size()==0) {
            sum+=Node[root].data*p*pow(1+r,depth);
            return;
        }
        for (int i=0;i<Node[root].child.size();i++) 
        DFS (Node[root].child[i],depth+1);
    }
    int main () {
        scanf ("%d%lf%lf",&n,&p,&r);
        r/=100;
        int k,child;
        for (int i=0;i<n;i++){
            scanf ("%d",&k);
            if (k==0) scanf ("%d",&Node[i].data);
            else {
                for (int j=0;j<k;j++) {
                    scanf ("%d",&child);
                    Node[i].child.push_back(child);
                }
            }
        }
        DFS(0,0);
        printf ("%.1f",sum);
    }
    View Code

    A1090 Highest Price in Supply Chain(25)

    #include<cstdio>
    #include<vector>
    #include<cmath>
    using namespace std;
    const int maxn=1000010;
    vector<int> Node[maxn];
    double highest=0;
    int highestnum=0;
    double price;
    int n;
    double p,r;
    void DFS (int root,int depth) {
        if (Node[root].size()==0) {
            price=p*pow(1+r,depth);
            if (price>highest) {
                highest=price;
                highestnum=1;
            }
            else if (price==highest) {
                highestnum++;
            }
            return;
        } 
        for (int i=0;i<Node[root].size();i++) 
        DFS (Node[root][i],depth+1);
    }
    int main () {
        scanf ("%d%lf%lf",&n,&p,&r);
        r/=100;
        int root;
        int father;
        for (int i=0;i<n;i++) {
            scanf ("%d",&father);
            if (father==-1) root=i;
            else Node[father].push_back(i);
        }
        DFS (root,0);
        printf ("%.2f %d",highest,highestnum);
        return 0;
    }
    View Code

    A1094 The Largest Generation(25)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    struct node {
        int data;
        vector<int> child;
    }Node[maxn];
    int d[maxn]={0},maxDepth=-1;
    void dfs (int root,int depth) {
        d[depth]++;
        maxDepth=max(depth,maxDepth);
        for (int i=0;i<Node[root].child.size();i++)
        dfs (Node[root].child[i],depth+1);
    }
    int main () {
        int N,M,K;
        int num,x;
        scanf ("%d %d",&N,&M);
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&num,&K);
            for (int j=0;j<K;j++) {
                scanf ("%d",&x);
                Node[num].child.push_back(x);
            }
        }
        dfs (1,1);
        int maxLength=0,u=-1;
        for (int i=1;i<=maxDepth;i++) {
            if (d[i]>maxLength) {
                maxLength=d[i];
                u=i;
            }
        }
        printf ("%d %d",maxLength,u);
        return 0;
    }
    View Code

    A1106 Lowest Price In Supply Train(25)

     #include <iostream>
     #include <cmath>
     #include <vector>
     using namespace std;
     vector<vector<int> > v;
     vector<int> level;
     int count=1,minlevel=100010;
     void dfs(int u,int l){
         if(v[u].size()==0){
             level[u]=l;
             if(minlevel>l){
                 count=1;
                 minlevel=l;
             }else if(minlevel==l){
                 count++;
             }
         }else{
             for(int w=0;w<v[u].size();w++){
                 dfs(v[u][w],l+1);
             }
         }    
     }
     int main(){
         int n;
         double price,rate;
         cin>>n>>price>>rate;
         v.resize(n),level.resize(n);
         for(int i=0;i<n;i++){
             int cnt;
             cin>>cnt;
             v[i].resize(cnt);
             for(int j=0;j<cnt;j++){
                 cin>>v[i][j];
             }
         }
         dfs(0,0);
         double ans=price*pow(1+rate/100.0,minlevel); 
         printf("%.4lf %d",ans,count);
         return 0;
     }
    View Code

    A1127 ZigZagging on a Tree(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    int post[maxn],in[maxn];
    struct node {
        int data;
        node * left;
        node * right;
    };
    node * create (int postL,int postR,int inL,int inR) {
        if (postL>postR) return NULL;
        node * root=new node;
        root->data=post[postR];
        int k;
        for (k=inL;k<=inR;k++)
        if (in[k]==post[postR]) break;
        int numLeft=k-inL;
        root->left=create (postL,postL+numLeft-1,inL,k-1);
        root->right=create (postL+numLeft,postR-1,k+1,inR);
        return root;
    }
    vector<int> d[maxn];
    int maxdepth=-1;
    void dfs (node * root,int depth) {
        if (root==NULL) return;
        maxdepth=max(maxdepth,depth);
        d[depth].push_back(root->data);
        dfs (root->left,depth+1);
        dfs (root->right,depth+1);
    }
    int N;
    int main () {
        scanf ("%d",&N);
        for (int i=0;i<N;i++)
        scanf ("%d",&in[i]);
        for (int i=0;i<N;i++)
        scanf ("%d",&post[i]);
        node * root=create (0,N-1,0,N-1);
        dfs (root,0);
        for (int i=0;i<=maxdepth;i++) {
            if (i%2==0) {
                for (int j=d[i].size()-1;j>=0;j--) {
                    if (j!=d[i].size()-1) printf (" ");
                    printf ("%d",d[i][j]);
                }
                if (i!=maxdepth) printf (" ");
            }
            else {
                for (int j=0;j<d[i].size();j++) {
                    if (j!=0) printf (" ");
                    printf ("%d",d[i][j]);
                }
                if (i!=maxdepth) printf (" ");
            }
        }
        return 0;
    }
    View Code

    专题三  二叉树的遍历

    A1020 Tree Traversals(25)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1001;
    struct node {
        int data;
        node * left=NULL;
        node * right=NULL;
    };
    int post[maxn],in[maxn];
    node * create (int postL,int postR,int inL,int inR) {
        if (postL>postR) return NULL;
        node * root=new node;
        root->data=post[postR];
        int k;
        for (k=inL;k<=inR;k++)
        if (in[k]==post[postR]) break;
        int numLeft=k-inL;
        root->left=create (postL,postL+numLeft-1,inL,k-1);
        root->right=create (postL+numLeft,postR-1,k+1,inR);
        return root;
    } 
    void bfs (node * root) {
        queue<node *> q;
        q.push (root);
        while (!q.empty()) {
            node * now=q.front();
            q.pop();
            if (now!=root) printf (" ");
            printf ("%d",now->data);
            if (now->left) q.push(now->left);
            if (now->right) q.push(now->right);
        }
    }
    int main () {
        int N;
        scanf ("%d",&N);
        for (int i=0;i<N;i++)
        scanf ("%d",&post[i]);
        for (int i=0;i<N;i++)
        scanf ("%d",&in[i]);
        node * root=create (0,N-1,0,N-1);
        bfs (root);
        return 0;
    }
    View Code

    A1043 Is It a BST(25)

    #include<cstdio>
    #include<vector>
    using namespace std;
    struct node {
        int data;
        node * lchild;
        node * rchild;
    }; 
    void insert (node * &root,int data) {
        if (root==NULL) {
            root=new node;
            root->data=data;
            root->lchild=root->rchild=NULL;
            return;
        }
        if (data<root->data)
        insert (root->lchild,data);
        else insert (root->rchild,data);
    }
    void preOrder (node * root,vector<int>& vi) {
        if (root==NULL) return;
        vi.push_back(root->data);
        preOrder (root->lchild,vi);
        preOrder (root->rchild,vi);
    }
    void preOrderMirror (node * root,vector<int>& vi) {
        if (root==NULL) return;
        vi.push_back(root->data);
        preOrderMirror (root->rchild,vi);
        preOrderMirror (root->lchild,vi);
    }
    void postOrder (node * root,vector<int>& vi) {
        if (root==NULL) return;
        postOrder (root->lchild,vi);
        postOrder (root->rchild,vi);
        vi.push_back(root->data);
    }
    void postOrderMirror (node * root,vector<int>& vi) {
        if (root==NULL) return;
        postOrderMirror (root->rchild,vi);
        postOrderMirror (root->lchild,vi);
        vi.push_back(root->data);
    }
    vector<int> origin,pre,preM,post,postM;
    int main () {
        int N,data;
        scanf ("%d",&N);
        node * root=NULL;
        for (int i=0;i<N;i++) {
            scanf ("%d",&data);
            origin.push_back(data);
            insert (root,data);
        }
        preOrder (root,pre);
        preOrderMirror (root,preM);
        postOrder (root,post);
        postOrderMirror (root,postM);
        if (origin==pre) {
            printf ("YES
    ");
            for (int i=0;i<N;i++) {
                printf ("%d",post[i]);
                if (i<N-1) printf (" ");
            }
        }
        else if (origin==preM) {
            printf ("YES
    ");
            for (int i=0;i<N;i++) {
                printf ("%d",postM[i]);
                if (i<N-1) printf (" ");
            }
        }
        else printf ("NO");
        return 0;
    }
    View Code

    A1064 CBT(30)

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn=1010;
    int n,number[maxn],CBT[maxn],index=0;
    void inOrder (int root) {
        if (root>n) return;
        inOrder(root*2);
        CBT[root]=number[index++];
        inOrder(root*2+1);
    }
    int main () {
        scanf ("%d",&n);
        for (int i=0;i<n;i++) {
            scanf ("%d",&number[i]);
        }
        sort (number,number+n);
        inOrder(1);
        for (int i=1;i<=n;i++) {
            printf ("%d",CBT[i]);
            if (i<n) printf (" ");
        }
        return 0;
    }
    View Code

    A1086 Tree Traversals Again(25)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    int pre[maxn],in[maxn];
    int cnt1=0,cnt2=0;
    stack<int> st;
    struct node {
        int data;
        node * left;
        node * right;
    };
    node * create (int preL,int preR,int inL,int inR) {
        if (preL>preR) return NULL;
        node * root=new node;
        root->data=pre[preL];
        int k;
        for (k=inL;k<=inR;k++) 
        if (in[k]==pre[preL]) break;
        int numLeft=k-inL;
        root->left=create(preL+1,preL+numLeft,inL,k-1);
        root->right=create(preL+numLeft+1,preR,k+1,inR);
        return root;
    }
    int num=0,N;
    void postOrder (node * root) {
        if (root==NULL) return;
        postOrder (root->left);
        postOrder (root->right);
        printf ("%d",root->data);
        num++;
        if (num<N) printf (" ");
    }
    int main () {
        scanf ("%d",&N);
        string s;
        int x;
        for (int i=0;i<N*2;i++) {
            cin>>s;
            if (s=="Push") {
                scanf ("%d",&x);
                st.push(x);
                pre[cnt1++]=x;
            }
            else {
                in[cnt2++]=st.top();
                st.pop();
            }
        }
        node * root=create(0,N-1,0,N-1);
        postOrder (root);
        return 0;
    }
    View Code

    A1099 Build A BST(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    struct node {
        int data;
        int left;
        int right;
    }Node[maxn];
    int a[maxn];
    int cnt=0;
    void inOrder (int root) {
        if (root==-1) return;
        inOrder (Node[root].left);
        Node[root].data=a[cnt++];
        inOrder (Node[root].right);
    }
    int num=0,N;
    void bfs (int root) {
        queue<int> q;
        q.push(root);
        while (!q.empty()) {
            int now=q.front();
            q.pop();
            printf ("%d",Node[now].data);
            num++;
            if (num<N) printf (" ");
            if (Node[now].left!=-1) q.push(Node[now].left);
            if (Node[now].right!=-1) q.push(Node[now].right);
        }
    }
    int main () {
        scanf ("%d",&N);
        int u,v;
        for (int i=0;i<N;i++) {
            scanf ("%d %d",&u,&v);
            Node[i].left=u;
            Node[i].right=v;
        }
        for (int i=0;i<N;i++)
        scanf ("%d",&a[i]);
        sort (a,a+N);
        inOrder (0);
        bfs (0);
        return 0;
    }
    View Code

    A1102 Invert a Binary Tree(25)

    #include<cstdio>
    #include<queue>
    #include<algorithm>
    using namespace std;
    const int maxn=110;
    struct node {
        int left;
        int right;
    }Node[maxn];
    int num,N;
    bool isRoot[maxn]={false};
    int strToint (char c) {
        if (c=='-') return -1;
        else {
        isRoot[c-'0']=true;
        return c-'0';}
    }
    void postOrder (int root) {
        if (root==-1) return;
        postOrder(Node[root].left);
        postOrder(Node[root].right);
        swap(Node[root].left,Node[root].right);
    }
    void BFS (int root) {
        queue<int> q;
        q.push(root);
        while (!q.empty()) {
            int now=q.front();
            q.pop();
            printf ("%d",now);
            num++;
            if (num<N) printf (" ");
            if (Node[now].left!=-1) q.push(Node[now].left);
            if (Node[now].right!=-1) q.push(Node[now].right);
        }
    }
    void inOrder (int root) {
        if (root==-1) return;
        inOrder(Node[root].left);
        printf ("%d",root);
        num++;
        if (num<N) printf (" ");
        inOrder(Node[root].right);
    }
    int main () {
        scanf ("%d",&N);
        char u,v;
        int left,right;
        for (int i=0;i<N;i++) {
            scanf ("%*c%c %c",&u,&v);
            Node[i].left=strToint(u);
            Node[i].right=strToint(v);
        }
        int root;
        for (root=0;root<N;root++)
        if (isRoot[root]==false) break;
        postOrder(root);
        num=0;
        BFS(root);
        printf ("
    ");
        num=0;
        inOrder(root);
        return 0;
    }
    View Code

    A1110 CBT(25)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    struct node {
        int left;
        int right;
    }Node[maxn];
    int lastindex;
    bool notRoot[maxn]={false};
    bool bfs (int root) {
        queue<int> q;
        q.push(root);
        int cnt=0;
        while (!q.empty()) {
            int now=q.front();
            q.pop();
            if (cnt!=0&&!(Node[now].left==-1&&Node[now].right==-1)) return false;
             else if (Node[now].left==-1&&Node[now].right!=-1) return false;
            else if ((Node[now].left!=-1&&Node[now].right==-1)||(Node[now].left==-1&&Node[now].right==-1)) {
                cnt++;
            }
            if (Node[now].left!=-1) q.push(Node[now].left);
            if (Node[now].right!=-1) q.push(Node[now].right); 
            if (q.empty()) lastindex=now; 
        }
        return true;
    } 
    int getnum (string s) {
        if (s=="-") return -1;
        int ans=0;
        for (int i=0;i<s.length();i++)
        ans=ans*10+s[i]-'0';
        return ans;
    }
    int main () {
        string s1,s2;
        int N;
        scanf ("%d",&N);
        for (int i=0;i<N;i++) {
            cin>>s1>>s2;
            Node[i].left=getnum(s1);
            Node[i].right=getnum(s2);
            if (Node[i].left!=-1) notRoot[Node[i].left]=true;
            if (Node[i].right!=-1) notRoot[Node[i].right]=true;
        }
        int root;
        for (root=0;root<N;root++)
        if (notRoot[root]==false) break;
        if (bfs(root)) printf ("YES %d",lastindex);
        else printf ("NO %d",root);
        return 0;
    }
    View Code

    A1130 Infix Expression(25)

    #include<iostream>
    #include<string>
    using namespace std;
    const int maxn=1014;
    struct node {
        string data;
        int left,right;
    }a[maxn];
    string dfs (int root) {
        if (a[root].left==-1&&a[root].right==-1) 
        return a[root].data;
        if (a[root].left==-1&&a[root].right!=-1) 
        return "("+a[root].data+dfs(a[root].right)+")";
        if (a[root].left!=-1&&a[root].right!=-1)
        return "("+dfs(a[root].left)+a[root].data+dfs(a[root].right)+")";
    }
    int main () {
        int isRoot[maxn]={0},n,root=1;
        cin>>n;
        for (int i=1;i<=n;i++) {
            cin>>a[i].data>>a[i].left>>a[i].right;
            if (a[i].left!=-1) isRoot[a[i].left]=1;
            if (a[i].right!=-1) isRoot[a[i].right]=1;
        }
        while (isRoot[root]==1)
        root++;
        string ans=dfs (root);
        if (ans[0]=='(') ans=ans.substr(1,ans.size()-2);
        cout<<ans;
        return 0;
    }
    View Code

    A1135 Is it A Red-black Tree(30)

    #include<iostream>
    #include<vector>
    #include<cmath>
    using namespace std;
    vector<int> arr;
    struct node {
        int data;
        node * left=NULL;
        node * right=NULL;
    };
    node * insert (node * &root,int v) {
        if (root==NULL) {
            root=new node;
            root->data=v;
        }
        else if (abs(v)<=abs(root->data))
        root->left=insert (root->left,v);
        else root->right=insert (root->right,v);
        return root;
    }
    bool judge1 (node * root) {
        if (root==NULL) return true;
        if (root->data<0) {
            if (root->left!=NULL&&root->left->data<0) return false;
            if (root->right!=NULL&&root->right->data<0) return false;
        }
        return judge1(root->left)&&judge1(root->right);
    }
    int getNum (node * root) {
        if (root==NULL) return 0;
        int left=getNum(root->left);
        int right=getNum(root->right);
        return root->data>0?max(left,right)+1:max(left,right);
    }
    bool judge2 (node * root) {
        if (root==NULL) return true;
        int left=getNum(root->left);
        int right=getNum(root->right);
        if (left!=right) return false;
        return judge2(root->left)&&judge2(root->right);
    }
    int main () {
        int k,n;
        scanf ("%d",&k);
        for (int i=0;i<k;i++) {
            scanf ("%d",&n);
            arr.resize(n);
            node * root=NULL;
            for (int j=0;j<n;j++) {
                scanf ("%d",&arr[j]);
                root=insert (root,arr[j]);
            }
            if (arr[0]<0||judge1(root)==false||judge2(root)==false) 
            printf ("No
    ");
            else printf ("Yes
    ");
        }
        return 0;
    }
    View Code

    A1143 LCA(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=10010;
    int a[maxn],M,N;
    unordered_map<int,int> pos;
    int main () {
        scanf ("%d %d",&M,&N);
        for (int i=0;i<N;i++) {
            scanf ("%d",&a[i]);
            pos[a[i]]=1;
        }
        int u,v;
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&u,&v);
            if (pos[u]==0&&pos[v]==0) printf ("ERROR: %d and %d are not found.
    ",u,v);
            else if (pos[u]==0||pos[v]==0) printf ("ERROR: %d is not found.
    ",pos[u]==0?u:v);
            else {
                int j;
                for (j=0;j<N;j++)
                if ((a[j]>=u&&a[j]<=v)||(a[j]<=u&&a[j]>=v)) break;
                if ((a[j]>u&&a[j]<v)||(a[j]>v&&a[j]<u)) printf ("LCA of %d and %d is %d.
    ",u,v,a[j]);
                else if (a[j]==u) printf ("%d is an ancestor of %d.
    ",u,v);
                else if (a[j]==v) printf ("%d is an ancestor of %d.
    ",v,u);
            }
        }
        return 0;
    }
    View Code

    A1147 Heaps(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    int a[maxn],N,T,num=0;
    void postOrder (int root) {
        if (root>N) return;
        postOrder (root*2);
        postOrder (root*2+1);
        printf ("%d",a[root]);
        num++;
        if (num<N) printf (" ");
    }
    int main () {
        scanf ("%d %d",&T,&N);
        while (T--) {
            num=0;
            for (int i=1;i<=N;i++) 
            scanf ("%d",&a[i]);
            int isMax=1,isMin=1;
            for (int i=2;i<=N;i++) {
                if (a[i/2]>a[i]) isMin=0;
                if (a[i/2]<a[i]) isMax=0;
            }
            if (isMax==1) printf ("Max Heap
    ");
            else if (isMin==1) printf ("Min Heap
    ");
            else printf ("Not Heap
    ");
            postOrder (1);
            printf ("
    ");
        }
        return 0;
    }
    View Code

    A1151 LCA in a Binary Tree(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=10010;
    struct node {
        int data;
        node * left;
        node * right;
    };
    int M,N;
    int pre[maxn],in[maxn];
    unordered_map<int,int> pos;
    int father[maxn*100];
    int depth[maxn*100];
    node * create (int preL,int preR,int inL,int inR) {
        if (preL>preR) return NULL;
        node * root=new node;
        root->data=pre[preL];
        int k;
        for (k=inL;k<=inR;k++)
        if (in[k]==pre[preL]) break;
        int numLeft=k-inL;
        root->left=create(preL+1,preL+numLeft,inL,k-1);
        if (root->left!=NULL) father[root->left->data]=root->data;
        root->right=create(preL+numLeft+1,preR,k+1,inR);
        if (root->right!=NULL) father[root->right->data]=root->data;
        return root;
    }
    void bfs (node * root) {
        queue<node *> q;
        q.push(root);
        depth[root->data]=1;
        while (!q.empty()) {
            node * now=q.front();
            q.pop();
            if (now->left) {q.push(now->left);depth[now->left->data]=depth[now->data]+1;}
            if (now->right) {q.push(now->right);depth[now->right->data]=depth[now->data]+1;}
        }
    }
    void lca (int u,int v) {
        int tu=u;
        int tv=v;
        while (depth[tu]<depth[tv]) {
            tv=father[tv];
        }
        while (depth[tu]>depth[tv]) {
            tu=father[tu];
        }
        while (tu!=tv) {
            tu=father[tu];
            tv=father[tv];
        }
        if (tu==u) printf ("%d is an ancestor of %d.
    ",u,v);
        else if (tu==v) printf ("%d is an ancestor of %d.
    ",v,u);
        else printf ("LCA of %d and %d is %d.
    ",u,v,tu); 
    }
    int main () {
        scanf ("%d %d",&M,&N);
        for (int i=1;i<=N;i++) father[i]=i;
        for (int i=1;i<=N;i++) {
            scanf ("%d",&in[i]);
            pos[in[i]]=i;
        }
        for (int i=1;i<=N;i++)
        scanf ("%d",&pre[i]);
        node * root=create(1,N,1,N);
        bfs(root);
        int u,v;
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&u,&v);
            if (pos[u]==0&&pos[v]==0) printf ("ERROR: %d and %d are not found.
    ",u,v);
            else if (pos[u]==0||pos[v]==0) printf ("ERROR: %d is not found.
    ",pos[u]==0?u:v);
            else lca (u,v);
        }
        return 0;
    }
    View Code

    A1155 Heap Paths(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    int a[maxn],N;
    vector<int> path;
    void dfs (int root) {
        if (root>N) return;
        path.push_back(root);
        if (root*2>N) {
            for (int i=0;i<path.size();i++) {
                if (i!=0) printf (" ");
                printf ("%d",a[path[i]]);
            }
            printf ("
    ");
            path.pop_back();
            return;
        }
        dfs (root*2+1);
        dfs (root*2);
        path.pop_back();
    }
    int main () {
        scanf ("%d",&N);
        for (int i=1;i<=N;i++)
        scanf ("%d",&a[i]);
        dfs (1);
        int isMax=1,isMin=1;
        for (int i=2;i<=N;i++) {
            if (a[i/2]>a[i]) isMin=0;
            if (a[i/2]<a[i]) isMax=0;
        } 
        if (isMin==1) printf ("Min Heap");
        else if (isMax==1) printf ("Max Heap");
        else printf ("Not Heap");
        return 0;
    }
    View Code

    专题四  深度优先搜索(DFS)

    A1013 Battle Over CIties(25)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    vector<int> g[maxn];
    bool visit[maxn]={false};
    int occupy,N,M,K;
    void dfs (int s) {
        if (s==occupy) return;
        visit[s]=true;
        for (int i=0;i<g[s].size();i++) 
        if (visit[g[s][i]]==false&&g[s][i]!=occupy) 
        dfs (g[s][i]);
    } 
    int dfsTrave () {
        int block=0;
        fill (visit,visit+maxn,false);
        for (int i=1;i<=N;i++) {
            if (visit[i]==false&&i!=occupy) {
                dfs (i);
                block++;
            }
        }
        return block-1;
    }
    int main () {
        scanf ("%d %d %d",&N,&M,&K);
        int u,v;
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        for (int i=0;i<K;i++) {
            scanf ("%d",&occupy);
            printf ("%d
    ",dfsTrave());
        }
        return 0;
    }
    View Code

    A1021 Deepest Root(25)

    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=100010;
    vector<int> g[maxn];
    bool visit[maxn]={false};
    int N;
    void dfs (int v) {
        visit[v]=true;
        for (int i=0;i<g[v].size();i++)
        if (visit[g[v][i]]==false)
        dfs (g[v][i]);
    } 
    int block=0;
    void dfstrave () {
        for (int i=1;i<=N;i++) 
        if (visit[i]==false) {
            dfs (i);
            block++;
        }
    }
    int maxheight=0,pre=-1;
    vector<int> v1,v2;
    void dfsfind (int s,int index,int pre) {
        if (index>maxheight) {
            v1.clear();
            v1.push_back(s);
            maxheight=index;
        }
        else if (index==maxheight) 
        v1.push_back(s);
        //visit[s]=true;
        for (int i=0;i<g[s].size();i++) {
        if (g[s][i]==pre) continue;
        dfsfind (g[s][i],index+1,s);
        }
    } 
    void dfsfind2 (int s,int index,int pre) {
        if (index>maxheight) {
            v2.clear();
            v2.push_back(s);
            maxheight=index;
        }
        else if (index==maxheight) 
        v2.push_back(s);
        //visit[s]=true;
        for (int i=0;i<g[s].size();i++) {
        if (g[s][i]==pre) continue;
        dfsfind2 (g[s][i],index+1,s);
        }
    } 
    int main () {
        scanf ("%d",&N);
        int u,v;
        for (int i=1;i<N;i++) {
            scanf ("%d %d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        dfstrave();
        if (block!=1) {
            printf ("Error: %d components",block);
            return 0;
        }
        dfsfind(1,1,-1);
        dfsfind2(v1[0],1,-1);
        for (int i=0;i<v2.size();i++)
        v1.push_back(v2[i]);
        int course[maxn]={0};
        sort (v1.begin(),v1.end());
        for (int i=0;i<v1.size();i++) {
            if (course[v1[i]]==0) {
            printf ("%d
    ",v1[i]);
            course[v1[i]]=1;
            }
        }
        return 0;
    }
    View Code

    A1103 Integer Factoriztion(30)

    #include<bits/stdc++.h>
    using namespace std;
    int N,K,P;
    vector<int> v,tmp,path;
    void init () {
        int tmp=0;
        int index=1;
        while (tmp<=N) {
            v.push_back(tmp);
            tmp=pow(index,P);
            index++;
        }
    }
    int maxFacSum=-1;
    void dfs (int nowindex,int nowK,int nowSum,int facSum) {
        if (nowK>K||nowSum>N) return;
        if (nowK==K) {
            if (nowSum==N) {
                if (facSum>maxFacSum) {
                    path=tmp;
                    maxFacSum=facSum;
                }
            }
            return;
        }
        while (nowindex>=1) {
            tmp.push_back(nowindex);
            dfs (nowindex,nowK+1,nowSum+v[nowindex],facSum+nowindex);
            tmp.pop_back();
            nowindex--;
            //if (nowindex==1) return;
        } 
    }
    int main () {
        scanf ("%d %d %d",&N,&K,&P);
        init ();
        dfs (v.size()-1,0,0,0);
        if (maxFacSum==-1) {
            printf ("Impossible");
            return 0;
        }
        printf ("%d = ",N);
        for (int i=0;i<path.size();i++) {
            if (i!=0) printf (" + ");
            printf ("%d^%d",path[i],P);
        }
        return 0;
    }
    View Code

    A1131 Subway Map(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=10010;
    const int inf=1e9;
    unordered_map<int,int> line;
    vector<int> g[maxn];
    vector<int> path,tmp;
    bool visit[maxn]={false};
    int N,st,ed;
    int transfer (vector<int> v) {
        int cnt=0;
        for (int i=1;i<v.size()-1;i++)
        if (line[v[i-1]*10000+v[i]]!=line[v[i]*10000+v[i+1]]) cnt++;
        return cnt;
    }
    int minCnt=1e9,minTransferCnt=1e9;
    void dfs (int s) {
        tmp.push_back(s);
        visit[s]=true;
        if (s==ed) {
            int transferCnt=transfer(tmp);
            if (tmp.size()<minCnt) {
                minCnt=tmp.size();
                minTransferCnt=transferCnt;
                path=tmp;
            }
            else if (tmp.size()==minCnt&&transferCnt<minTransferCnt) {
                minTransferCnt=transferCnt;
                path=tmp;
            }
            tmp.pop_back();
            visit[s]=false;
            return;
        }
        for (int i=0;i<g[s].size();i++)
        if (visit[g[s][i]]==false) dfs (g[s][i]);
        tmp.pop_back();
        visit[s]=false;
    }
    int main () {
        scanf ("%d",&N);
        int k,u,v;
        for (int i=1;i<=N;i++) {
            scanf ("%d %d",&k,&u);
            for (int j=1;j<k;j++) {
                scanf ("%d",&v);
                g[u].push_back(v);
                g[v].push_back(u);
                line[u*10000+v]=line[v*10000+u]=i;
                u=v;
            }
        }
        int q;
        scanf ("%d",&q);
        for (int i=0;i<q;i++) {
            fill (visit,visit+maxn,false);
            minCnt=1e9,minTransferCnt=1e9;
            scanf ("%d %d",&st,&ed);
            dfs (st);
            printf ("%d
    ",path.size()-1);
            int preLine=line[path[0]*10000+path[1]],pre=path[0];
            for (int j=1;j<path.size();j++) {
                if (line[path[j-1]*10000+path[j]]!=preLine) {
                    printf ("Take Line#%d from %04d to %04d.
    ",preLine,pre,path[j-1]);
                    pre=path[j-1];
                    preLine=line[path[j-1]*10000+path[j]];
                }
            }
            printf ("Take Line#%d from %04d to %04d.
    ",preLine,pre,ed);
        }
        return 0;
    }
    View Code

    A1148 Werewolf(20)

    #include<stdio.h>
    #include<math.h>
    int ans[110];
    int judge[110];
    int N;
    int main () {
        scanf ("%d",&N);
        for (int i=1;i<=N;i++) 
        scanf ("%d",&judge[i]);
        int lie,cnt;
        for (int i=1;i<N;i++) {
            for (int j=i+1;j<=N;j++) {
                lie=0;cnt=0;
                for (int w=1;w<=N;w++) 
                ans[w]=1;
                ans[i]=-1;ans[j]=-1;
                for (int w=1;w<=N;w++) 
                if (judge[w]*ans[abs(judge[w])]<0) {
                    lie++;
                    if (w==i||w==j) cnt++;
                }
                if (lie==2&&cnt==1) {
                    printf ("%d %d",i,j);
                    return 0;
                }
            }
        }
        printf ("No Solution");
        return 0;
    }
    View Code

    专题五  广度优先搜索(BFS)

    A1076 Forwards on Weibo(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    vector<int> g[maxn];
    struct node {
        int id,layer;
    };
    int N,L;
    int bfs (int s) {
        int cnt=0;
        bool visit[maxn]={false};
        visit[s]=true;
        queue<node> q;
        q.push({s,0});
        while (!q.empty()) {
            node now=q.front();
            q.pop();
            int id=now.id;
            int layer=now.layer;
            if (layer<L) {
                for (int i=0;i<g[id].size();i++) {
                    if (visit[g[id][i]]==false) {
                        q.push({g[id][i],layer+1});
                        cnt++;
                        visit[g[id][i]]=true;
                    }
                }
            }
        }
        return cnt;
    }
    int main () {
        scanf ("%d %d",&N,&L);
        int k,x;
        for (int i=1;i<=N;i++) {
            scanf ("%d",&k);
            for (int j=0;j<k;j++) {
                scanf ("%d",&x);
                g[x].push_back(i);
            }
        }
        int q;
        scanf ("%d",&q);
        for (int i=0;i<q;i++) {
            scanf ("%d",&x);
            printf ("%d
    ",bfs(x));
        }
        return 0;
    }
    View Code

    A1091 Acute Stroke(30)

    #include<bits/stdc++.h>
    using namespace std;
    int n,m,l,t;
    bool visit[1300][130][80]={false};
    int adj[1300][130][80];
    int X[6]={1,0,0,-1,0,0};
    int Y[6]={0,1,0,0,-1,0};
    int Z[6]={0,0,1,0,0,-1};
    struct node {
        int x,y,z;
    };
    int judge (int x,int y,int z) {
        if (x>=m||x<0||y>=n||y<0||z>=l||z<0) 
        return 0;
        if (visit[x][y][z]==true||adj[x][y][z]==0) return 0;
        return 1;
    }
    int bfs (int x,int y,int z) {
        queue<node> q;
        int cnt=1;
        q.push({x,y,z});
        visit[x][y][z]=true;
        while (!q.empty()) {
            node now=q.front();
            q.pop();
            for (int i=0;i<6;i++) {
                int tx=now.x+X[i];
                int ty=now.y+Y[i];
                int tz=now.z+Z[i];
                if (judge(tx,ty,tz)) {
                    q.push({tx,ty,tz});
                    visit[tx][ty][tz]=true;
                    cnt++;
                }
            }
        }
        if (cnt<t) return 0;
        return cnt;
    }
    int main () {
        scanf ("%d %d %d %d",&m,&n,&l,&t);
        for (int i=0;i<l;i++)
        for (int j=0;j<m;j++)
        for (int k=0;k<n;k++)
        scanf ("%d",&adj[j][k][i]);
        int cnt=0;
        for (int i=0;i<l;i++)
        for (int j=0;j<m;j++)
        for (int k=0;k<n;k++)
        if (visit[j][k][i]==false&&adj[j][k][i]==1) {
            cnt+=bfs(j,k,i);
        }
        printf ("%d
    ",cnt);
        return 0;
    }
    View Code

    专题六  最短路径

    A1003 Emergency(25)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1014;
    const int inf=1e9;
    int g[maxn][maxn],d[maxn],w[maxn],num[maxn],weight[maxn]; 
    bool visit[maxn];
    int N,M,st,ed;
    void init () {
        for (int i=0;i<N;i++)
        for (int j=0;j<N;j++)
        g[i][j]=inf;
    }
    void dijkstra (int s) {
        fill (d,d+maxn,inf);
        fill (w,w+maxn,0);
        fill (num,num+maxn,0);
        fill (visit,visit+maxn,false);
        d[s]=0;
        w[s]=weight[s];
        num[s]=1;
        for (int i=0;i<N;i++) {
            int u=-1,min=inf;
            for (int j=0;j<N;j++)
            if (visit[j]==false&&d[j]<min) {
                u=j;
                min=d[j];
            }
            if (u==-1) return;
            visit[u]=true;
            for (int v=0;v<N;v++) {
                if (visit[v]==false&&g[u][v]!=inf) {
                    if (d[u]+g[u][v]<d[v]) {
                        d[v]=d[u]+g[u][v];
                        w[v]=w[u]+weight[v];
                        num[v]=num[u];
                    }
                    else if (d[u]+g[u][v]==d[v]) {
                        num[v]+=num[u];
                        if (w[u]+weight[v]>w[v]) 
                        w[v]=w[u]+weight[v];
                    }
                }
            }
        }
    }
    int main () {
        scanf ("%d %d %d %d",&N,&M,&st,&ed);
        for (int i=0;i<N;i++)
        scanf ("%d",&weight[i]);
        init ();
        int u,v;
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&u,&v);
            scanf ("%d",&g[u][v]);
            g[v][u]=g[u][v];
        }
        dijkstra (st);
        printf ("%d %d",num[ed],w[ed]);
        return 0;
    }
    View Code

    A1018 Public Bike Management(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1014;
    const int inf=1e9;
    int g[maxn][maxn],d[maxn],weight[maxn];
    bool visit[maxn]={false};
    vector<int> pre[maxn];
    vector<int> path,tmp;
    int Cmax,N,Sp,M;
    void init () {
        for (int i=0;i<=N;i++)
        for (int j=0;j<=N;j++)
        g[i][j]=inf;
    }
    void dijkstra (int s) {
        fill (d,d+maxn,inf);
        fill (visit,visit+maxn,false);
        d[s]=0;
        for (int i=0;i<=N;i++) {
            int u=-1,min=inf;
            for (int j=0;j<=N;j++)
            if (visit[j]==false&&d[j]<min) {
                u=j;
                min=d[j];
            }
            if (u==-1) return;
            visit[u]=true;
            for (int v=0;v<=N;v++) {
                if (visit[v]==false&&g[u][v]!=inf) {
                    if (d[u]+g[u][v]<d[v]) {
                        d[v]=d[u]+g[u][v];
                        pre[v].clear();
                        pre[v].push_back(u);
                    }
                    else if (d[u]+g[u][v]==d[v])
                    pre[v].push_back(u);
                }
            }
        }
    }
    int minNeed,minBack;
    void dfs (int v) {
        tmp.push_back(v);
        if (v==0) {
            int need=0,back=0;
            for (int i=tmp.size()-1;i>=0;i--) {
                int id=tmp[i];
                if (weight[id]>0) back+=weight[id];
                else {
                    if (back>(0-weight[id])) back+=weight[id];
                    else {
                        need+=(0-weight[id])-back;
                        back=0;
                    }
                 } 
            }
            if (need<minNeed) {
                minNeed=need;
                minBack=back;
                path=tmp;
            }
            else if (need==minNeed&&back<minBack) {
                minBack=back;
                path=tmp;
            }
            tmp.pop_back();
            return;
        }
        for (int i=0;i<pre[v].size();i++) 
        dfs (pre[v][i]);
        tmp.pop_back();
    }
    int main () {
        scanf ("%d %d %d %d",&Cmax,&N,&Sp,&M);
        for (int i=1;i<=N;i++) {
            scanf ("%d",&weight[i]);
            weight[i]-=Cmax/2;
        }
        int u,v;
        init ();
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&u,&v);
            scanf ("%d",&g[u][v]);
            g[v][u]=g[u][v];
        }
        dijkstra (0);
        minNeed=1e9;
        minBack=1e9;
        dfs (Sp);
        printf ("%d ",minNeed);
        for (int i=path.size()-1;i>=0;i--) {
            if (i!=path.size()-1) printf ("->");
            printf ("%d",path[i]);
         }
         printf (" %d",minBack);
         return 0;
    }
    View Code

    A1030 Travel Plan(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    const int inf=1e9;
    int g[maxn][maxn],cost[maxn][maxn],d[maxn],c[maxn];
    int pre[maxn];
    bool visit[maxn];
    int N,M,st,ed;
    void init () {
        for (int i=0;i<N;i++)
        for (int j=0;j<N;j++)
        g[i][j]=inf;
    }
    void dijkstra (int s) {
        fill (d,d+maxn,inf);
        fill (c,c+maxn,inf);
        d[s]=0;
        c[s]=0;
        for (int i=0;i<N;i++) {
            int u=-1,min=inf;
            for (int j=0;j<N;j++)
            if (visit[j]==false&&d[j]<min) {
                u=j;
                min=d[j];
            }
            if (u==-1) return;
            visit[u]=true;
            for (int v=0;v<N;v++) {
                if (visit[v]==false&&g[u][v]!=inf) {
                    if (d[u]+g[u][v]<d[v]) {
                        d[v]=d[u]+g[u][v];
                        c[v]=c[u]+cost[u][v];
                        pre[v]=u;
                    }
                    else if (d[u]+g[u][v]==d[v]&&c[u]+cost[u][v]<c[v]) {
                        c[v]=c[u]+cost[u][v];
                        pre[v]=u;
                    }
                }
            }
        }
    } 
    void dfs (int v) {
        if (v==st) {
            printf ("%d",v);
            return;
        }
        dfs (pre[v]);
        printf (" %d",v);
    }
    int main () {
        scanf ("%d %d %d %d",&N,&M,&st,&ed);
        init ();
        int u,v;
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&u,&v);
            scanf ("%d %d",&g[u][v],&cost[u][v]);
            g[v][u]=g[u][v];
            cost[v][u]=cost[u][v];
        }
        dijkstra (st);
        dfs (ed);
        printf (" %d %d",d[ed],c[ed]);
        return 0;
    }
    View Code

    A1072 Gas Station(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1011;
    const int inf=1e9;
    int d[maxn][maxn];
    int N,M,K,D;
    void init () {
        for (int i=1;i<=N+M;i++)
        for (int j=1;j<=N+M;j++)
        d[i][j]=inf;
    }
    void floyd () {
        for (int k=1;k<=N+M;k++)
        for (int i=N+1;i<=N+M;i++)
        for (int j=1;j<=i;j++) {
            d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
            d[j][i]=d[i][j];
        }
    }
    int getnum (string s) {
        int flag=0;
        if (s[0]=='G') flag++;
        int ans=0;
        for (int i=flag;i<s.length();i++)
        ans=ans*10+s[i]-'0';
        if (flag==0) return ans;
        else return ans+N;
    }
    int main () {
        scanf ("%d %d %d %d",&N,&M,&K,&D);
        string s1,s2;
        int u,v;
        init ();
        for (int i=0;i<K;i++) {
            cin>>s1>>s2;
            u=getnum(s1);
            v=getnum(s2);
            scanf ("%d",&d[u][v]);
            d[v][u]=d[u][v];
        }
        floyd ();
        int maxDistance=-1;double minAvg=inf;
        for (int i=N+1;i<=N+M;i++) {
            int flag=0,minDistance=inf;double avg=0;
            for (int j=1;j<=N;j++) {
                if (d[i][j]>D) flag++;
                if (d[i][j]<minDistance) {
                    minDistance=d[i][j];
                }
                avg+=d[i][j];
            }
            if (flag!=0) continue;
            avg/=N;
            if (minDistance>maxDistance) {
                maxDistance=minDistance;
                minAvg=avg;
                u=i;
            }
            else if (minDistance==maxDistance&&avg<minAvg) {
                minAvg=avg;
                u=i;
            }
        }
        if (maxDistance==-1) printf ("No Solution");
        else printf ("G%d
    %.1f %.1f",u-N,(double)maxDistance,minAvg);
        return 0;
    }
    View Code

    A1087 All Roads Lead to Rome(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    const int inf=1e9;
    unordered_map<string,int> pos1;
    unordered_map<int,string> pos2;
    int g[maxn][maxn],d[maxn],w[maxn],weight[maxn],pre[maxn],num[maxn],numNode[maxn];
    bool visit[maxn]={false};
    int N,M;
    void init () {
        for (int i=1;i<=N;i++)
        for (int j=1;j<=N;j++) 
        g[i][j]=inf;
    } 
    void dijkstra (int s) {
        fill (d,d+maxn,inf);
        fill (visit,visit+maxn,false);
        fill (w,w+maxn,0);
        fill (num,num+maxn,0);
        fill (numNode,numNode+maxn,inf);
        num[s]=1;
        d[s]=0;
        w[s]=weight[s];
        numNode[s]=0;
        for (int i=1;i<=N;i++) {
            int u=-1,min=inf;
            for (int j=1;j<=N;j++)
            if (visit[j]==false&&d[j]<min) {
                u=j;
                min=d[j];
            }
            if (u==-1) return;
            visit[u]=true;
            for (int v=1;v<=N;v++) {
                if (visit[v]==false&&g[u][v]!=inf) {
                    if (d[u]+g[u][v]<d[v]) {
                        d[v]=d[u]+g[u][v];
                        pre[v]=u;
                        w[v]=w[u]+weight[v];
                        num[v]=num[u];
                        numNode[v]=numNode[u]+1;
                    }
                    else if (d[u]+g[u][v]==d[v]) {
                        num[v]+=num[u];
                        if (w[u]+weight[v]>w[v]) {
                            w[v]=w[u]+weight[v];
                            pre[v]=u;
                            numNode[v]=numNode[u]+1;
                        }
                        else if (w[u]+weight[v]==w[v]&&numNode[u]+1<numNode[v]) {
                            numNode[v]=numNode[u]+1;
                            pre[v]=u;
                        } 
                    }
                }
            }
        }
    }
    void dfs (int v) {
        if (v==1) {
            cout<<pos2[v];
            return;
        }
        dfs (pre[v]);
        cout<<"->"<<pos2[v];
    }
    int main () {
        scanf ("%d %d",&N,&M);
        string s1,s2;
        int x;
        cin>>s1;
        pos1[s1]=1;
        pos2[1]=s1;
        for (int i=2;i<=N;i++) {
            cin>>s1>>x;
            pos1[s1]=i;
            pos2[i]=s1;
            weight[i]=x;
        }
        init ();
        for (int i=0;i<M;i++) {
            cin>>s1>>s2>>x;
            g[pos1[s1]][pos1[s2]]=g[pos1[s2]][pos1[s1]]=x;
        }
        dijkstra (1);
        int ed=pos1["ROM"];
        printf ("%d %d %d %d
    ",num[ed],d[ed],w[ed],w[ed]/numNode[ed]);
        dfs (ed);
        return 0;
    }
    View Code

    A1111 Online Map(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    const int inf=1e9;
    int g[maxn][maxn],cost[maxn][maxn],d[maxn],c[maxn],numNode[maxn];
    bool visit[maxn]={false};
    int pre[maxn];
    vector<int> pre1,pre2;
    int N,M,st,ed;
    void init () {
        for (int i=0;i<N;i++) 
        for (int j=0;j<N;j++) {
            g[i][j]=inf;
            cost[i][j]=inf;
        }
    }
    void dijkstra1 (int s) {
        fill (d,d+maxn,inf);
        fill (c,c+maxn,inf);
        fill (visit,visit+maxn,false);
        d[s]=0;
        c[s]=0;
        for (int i=0;i<N;i++) {
            int u=-1,min=inf;
            for (int j=0;j<N;j++) 
            if (visit[j]==false&&d[j]<min) {
                u=j;
                min=d[j];
            }
            if (u==-1) return;
            visit[u]=true;
            for (int v=0;v<N;v++) {
                if (visit[v]==false&&g[u][v]!=inf) {
                    if (d[u]+g[u][v]<d[v]) {
                        d[v]=d[u]+g[u][v];
                        c[v]=c[u]+cost[u][v];
                        pre[v]=u;
                    }
                    else if (d[u]+g[u][v]==d[v]&&c[u]+cost[u][v]<c[v]) {
                        c[v]=c[u]+cost[u][v];
                        pre[v]=u;
                    }
                }
            }
        }
    }
    void dfs1 (int v) {
        if (v==st) {
            pre1.push_back(v);
            return;
        }
        dfs1 (pre[v]);
        pre1.push_back(v);
    }
    void dijkstra2 (int s) {
        fill (c,c+maxn,inf);
        fill (numNode,numNode+maxn,inf);
        fill (visit,visit+maxn,false);
        c[s]=0;
        numNode[s]=0;
        for (int i=0;i<N;i++) {
            int u=-1,min=inf;
            for (int j=0;j<N;j++)
            if (visit[j]==false&&c[j]<min) {
                u=j;
                min=c[j];
            }
            if (u==-1) return;
            visit[u]=true;
            for (int v=0;v<N;v++) {
                if (visit[v]==false&&cost[u][v]!=inf) {
                    if (c[u]+cost[u][v]<c[v]) {
                        c[v]=c[u]+cost[u][v];
                        numNode[v]=numNode[u]+1;
                        pre[v]=u;
                    }
                    else if (c[u]+cost[u][v]==c[v]&&numNode[u]+1<numNode[v]) {
                        numNode[v]=numNode[u]+1;
                        pre[v]=u;
                    }
                }
            }
        }
    }
    void dfs2 (int v) {
        if (v==st) {
            pre2.push_back(v);
            return;
        }
        dfs2 (pre[v]);
        pre2.push_back(v);
    }
    int main () {
        scanf ("%d %d",&N,&M);
        int u,v,flag;
        init ();
        for (int i=0;i<M;i++) {
            scanf ("%d %d %d",&u,&v,&flag);
            scanf ("%d %d",&g[u][v],&cost[u][v]);
            if (flag==0) {
                g[v][u]=g[u][v];
                cost[v][u]=cost[u][v];
            }
        }
        scanf ("%d %d",&st,&ed);
        dijkstra1 (st);
        dfs1 (ed);
        int d1=d[ed];
        dijkstra2 (st);
        dfs2 (ed);
        int t1=c[ed];
        if (pre1==pre2) {
            printf ("Distance = %d; Time = %d: ",d1,t1);
            for (int i=0;i<pre1.size();i++) {
                if (i!=0) printf (" -> ");
                printf ("%d",pre1[i]);
            }
            return 0;
        }
        printf ("Distance = %d: ",d1);
        for (int i=0;i<pre1.size();i++) {
                if (i!=0) printf (" -> ");
                printf ("%d",pre1[i]);
        }
        printf ("
    ");
        printf ("Time = %d: ",t1);
        for (int i=0;i<pre2.size();i++) {
            if (i!=0) printf (" -> ");
            printf ("%d",pre2[i]);
        }
        return 0;
    }
    View Code

    专题七  平衡二叉树

    A1066 Root of AVL Tree(25)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    struct node {
        int data;
        node * left=NULL;
        node * right=NULL;
        int height;
    };
    int getHeight (node * root) {
        if (root==NULL) return 0;
        return root->height;
    }
    int getBalance (node * root) {
        return getHeight(root->left)-getHeight(root->right);
    }
    void update (node * &root) {
        root->height=max(getHeight(root->left),getHeight(root->right))+1;
    }
    void L (node * &root) {
        node * t=root->right;
        root->right=t->left;
        t->left=root;
        update (root);
        update (t);
        root=t;
    }
    void R (node * &root) {
        node * t=root->left;
        root->left=t->right;
        t->right=root;
        update (root);
        update (t);
        root=t;
    }
    void insert (node * &root,int x) {
        if (root==NULL) {
            root=new node;
            root->data=x;
            return;
        }
        if (x<=root->data) {
            insert (root->left,x);
            update (root);
            update (root->left);
            if (getBalance(root)==2) {
                if (getBalance(root->left)==1) 
                R (root);
                else if (getBalance(root->left)==-1) {
                    L (root->left);
                    R (root);
                }
            }
        }
        else {
            insert (root->right,x);
            update (root);
            update (root->right);
            if (getBalance(root)==-2) {
                if (getBalance(root->right)==-1) 
                L (root);
                else if (getBalance(root->right)==1) {
                    R (root->right);
                    L (root);
                }
            }
        }
    }
    int main () {
        int N;
        node * root=NULL;
        scanf ("%d",&N);
        int x;
        for (int i=0;i<N;i++) {
            scanf ("%d",&x);
            insert (root,x);
        }
        printf ("%d",root->data);
        return 0;
    }
    View Code

    A1123 Is It A complete AVL Tree(30)

    #include<cstdio>
    #include<algorithm>
    #include<queue> 
    #include<vector>
    using namespace std;
    struct node {
        int v;
        int height;
        node * left;
        node * right;
    }; 
    node * newNode (int v) {
        node * Node=new node;
        Node->v=v;
        Node->height=1;
        Node->left=NULL;
        Node->right=NULL;
        return Node;
    }
    int getHeight (node * root) {
        if (root==NULL) return 0;
        return root->height;
    }
    void update (node * &root) {
        root->height=max(getHeight(root->left),getHeight(root->right))+1;
    }
    int getBalance (node * root) {
        return getHeight(root->left)-getHeight(root->right);
    }
    void L (node * &root) {
        node * temp=root->right;
        root->right=temp->left;
        temp->left=root;
        update(root);
        update(temp);
        root=temp;
    }
    void R (node * &root) {
        node * temp=root->left;
        root->left=temp->right;
        temp->right=root;
        update(root);
        update(temp);
        root=temp;
    }
    void insert (node * &root,int x) {
        if (root==NULL) {
            root=newNode(x);
            return;
        }
        if (x<root->v) {
            insert (root->left,x);
            update(root);
            if (getBalance(root)==2) {
                if (getBalance(root->left)==1) 
                R(root);
                else if (getBalance(root->left)==-1) {
                    L(root->left);
                    R(root);
                }
            }
         }
         else {
             insert (root->right,x);
             update(root);
             if (getBalance(root)==-2) {
                 if (getBalance(root->right)==-1) 
                 L(root);
                 else if (getBalance(root->right)==1) {
                     R(root->right);
                     L(root);
                 }
             }
         }
    }
    int num=0,N;
    vector<int> vi;
    int flag=0,cnt=0;
    void BFS (node * root) {
        queue<node *> q;
        q.push(root);
        while (!q.empty()) {
            node * now=q.front();
            q.pop();
            vi.push_back(now->v);
            if (!(!now->left&&!now->right)&&cnt==1) 
            flag=1;
            if (!now->left&&now->right) 
            flag=1;
            if ((now->left&&!now->right)||(!now->left&&!now->right)) 
            cnt++;
            if (now->left) q.push(now->left);
            if (now->right) q.push(now->right);
        }
    }
    int main () {
        int N;
        scanf ("%d",&N);
        int x;
        node * root=NULL;
        for (int i=0;i<N;i++) {
            scanf ("%d",&x);
            insert (root,x);
        }
        BFS (root);
        for (int i=0;i<vi.size();i++) {
            printf ("%d",vi[i]);
            if (i<vi.size()-1) printf (" ");
        }
        printf ("
    ");
        if (flag==1) printf ("NO");
        else printf ("YES");
        return 0;
    }
    View Code

    专题八  堆排序

    A1089 Insert or Merge(25)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    int a[maxn],b[maxn];
    int i,j,n;
    void merge () {
        int k=1;
        while (k<n) {
            int flag=0;
            for (int i=0;i<n;i++)
            if (a[i]!=b[i]) flag=1;
            k*=2;
            for (int i=0;i<n/k;i++) {
                sort (a+i*k,a+(i+1)*k);
            }
            sort (a+n/k*k,a+n);
            if (flag==0) break;
        }
    }
    int main () {
        scanf ("%d",&n);
        for (i=0;i<n;i++)
        scanf ("%d",&a[i]);
        for (i=0;i<n;i++)
        scanf ("%d",&b[i]);
        for (i=0;i<n-1&&b[i]<=b[i+1];i++);
        for (j=i+1;j<n&&a[j]==b[j];j++);
        if (j==n) {
            printf ("Insertion Sort
    ");
            sort (a,a+i+2);
        } 
        else {
            printf ("Merge Sort
    ");
            merge ();
        }
        for (i=0;i<n;i++) {
            if (i!=0) printf (" ");
            printf ("%d",a[i]);
        }
        return 0;
    }
    View Code

    A1098 Insertioin or Heap sort(25)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    int a[maxn],b[maxn],n;
    void downAdjust (int low,int high) {
        int i=low,j=i*2;
        while (j<=high) {
            if (j+1<=high&&a[j+1]>a[j])
            j=j+1;
            if (a[j]>a[i]) {
                swap (a[j],a[i]);
               i=j;
               j=i*2;
            }
            else break;
        }
    }
    void createHeap () {
        for (int i=n/2;i>=1;i--)
        downAdjust (i,n);
    }
    void heapSort () {
        createHeap ();
        int flag=1;
        for (int i=n;i>1&&flag;i--) {
            flag=0;
            for (int j=1;j<=n;j++)
            if (a[j]!=b[j]) flag=1;
            swap (a[i],a[1]);
            downAdjust (1,i-1);
        }
    }
    int main () {
        scanf ("%d",&n);
        int i,j;
        for (i=1;i<=n;i++)
        scanf ("%d",&a[i]);
        for (i=1;i<=n;i++)
        scanf ("%d",&b[i]);
        for (i=1;i<n&&b[i]<=b[i+1];i++);
        for (j=i+1;j<=n&&a[j]==b[j];j++);
        if (j==n+1) {
            printf ("Insertion Sort
    ");
            sort (a+1,a+i+2);
        }
        else {
            printf ("Heap Sort
    ");
            heapSort ();
        }
        for (i=1;i<=n;i++) {
            if (i!=1) printf (" ");
            printf ("%d",a[i]);
        }
        return 0;
    }
    View Code

    专题九  简单图论

    A1134 Vertex Cover(25)

    #include<iostream>
    #include<vector>
    using namespace std;
    int main () {
        int n,m,k,nv,a,b,num;
        scanf ("%d %d",&n,&m);
        vector<int> v[n];
        for (int i=0;i<m;i++) {
            scanf ("%d%d",&a,&b);
            v[a].push_back(i);
            v[b].push_back(i);
        }
        scanf ("%d",&k);
        for (int i=0;i<k;i++) {
            scanf ("%d",&nv);
            int flag=0;
            vector<int> hash(m,0);
            for (int j=0;j<nv;j++) {
                scanf ("%d",&num);
                for (int t=0;t<v[num].size();t++)
                hash[v[num][t]]=1;
            }
            for (int j=0;j<m;j++)
            if (hash[j]==0) {
                printf ("No
    ");
                flag=1;
                break;
            }
            if (flag==0) printf ("Yes
    ");
        }
        return 0;
    } 
    View Code

    A1139 First Contact(30)

    #include<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<unordered_map>
    using namespace std;
    unordered_map<int,bool> arr;
    struct node {
        int a,b;
    };
    bool cmp (node x,node y) {
        return x.a!=y.a?x.a<y.a:x.b<y.b;
    }
    int main () {
        int n,m,k;
        scanf ("%d %d",&n,&m);
        vector<int> v[10010];
        for (int i=0;i<m;i++) {
            string a,b;
            cin>>a>>b;
            if (a.length()==b.length()) {
                v[abs(stoi(a))].push_back(abs(stoi(b)));
                v[abs(stoi(b))].push_back(abs(stoi(a)));
            }
            arr[abs(stoi(a))*10000+abs(stoi(b))]=
            arr[abs(stoi(b))*10000+abs(stoi(a))]=true;
        }
        scanf ("%d",&k);
        for (int i=0;i<k;i++) {
            int c,d;
            cin>>c>>d;
            vector<node> ans;
            for (int j=0;j<v[abs(c)].size();j++) {
                for (int k=0;k<v[abs(d)].size();k++) {
                    if (v[abs(c)][j]==abs(d)||v[abs(d)][k]==abs(c)) 
                    continue;
                    if (arr[v[abs(c)][j]*10000+v[abs(d)][k]]==true)
                    ans.push_back(node{v[abs(c)][j],v[abs(d)][k]});
                }
            }
            sort (ans.begin(),ans.end(),cmp);
            printf ("%d
    ",(int)ans.size());
            for (int j=0;j<ans.size();j++) 
            printf ("%04d %04d
    ",ans[j].a,ans[j].b);
        }
        return 0;
    }
    View Code

    A1142 Maximal Clique(25)

    #include<cstdio>
    #include<vector>
    using namespace std;
    const int maxn=510;
    int g[maxn][maxn]={0};
    int main () {
        int nv,ne,m,ta,tb,k;
        scanf ("%d %d",&nv,&ne);
        for (int i=0;i<ne;i++) {
            scanf ("%d %d",&ta,&tb);
            g[ta][tb]=g[tb][ta]=1;
        }
        scanf ("%d",&m);
        for (int i=0;i<m;i++) {
            scanf ("%d",&k);
            vector<int> v(k);
            int hash[maxn]={0};
            int isclique=1;
            int isMaximal=1;
            for (int j=0;j<k;j++) {
                scanf ("%d",&v[j]);
                hash[v[j]]=1;
            }
            for (int j=0;j<k;j++) {
                if (isclique==0) break;
                for (int l=j+1;l<k;l++) 
                if (g[v[j]][v[l]]==0) {
                    isclique=0;
                    printf ("Not a Clique
    ");
                    break;
                }
            }
            if (isclique==0) continue;
            for (int j=1;j<=nv;j++) {
                if (hash[j]==0) {
                    for (int l=0;l<k;l++) {
                        if (g[v[l]][j]==0) break;
                        if (l==k-1) isMaximal=0;
                    }
                }
                if (isMaximal==0) {
                    printf ("Not Maximal
    ");
                    break;
                }
            }
            if (isMaximal==1) printf ("Yes
    ");
        }
        return 0;
    }
    View Code

    A1146 Topological Order(25)

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int maxn=1014;
    const int inf=1e9;
    int main () {
        int N,M;
        vector<int> g[maxn];
        int inDegree[maxn];
        scanf ("%d %d",&N,&M);
        int u,v;
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&u,&v);
            g[u].push_back(v);
            inDegree[v]++;
        }
        int a[maxn],t[maxn];
        int k;
        scanf ("%d",&k);
        vector<int> vi;
        for (int i=1;i<=k;i++) {
            for (int j=1;j<=N;j++)
            t[j]=inDegree[j];
            int flag=0,x;
            for (int j=1;j<=N;j++) {
                scanf ("%d",&x);
                if (t[x]!=0) flag++;
                for (int w=0;w<g[x].size();w++) 
                t[g[x][w]]--; 
            }
            if (flag!=0) vi.push_back(i);
        }
        for (int i=0;i<vi.size();i++) {
            if (i!=0) printf (" ");
            printf ("%d",vi[i]-1);
        }
        return 0;
    }
    View Code

    专题十  并查集

    A1034 Head of Gang(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=10010;
    int father[maxn],isRoot[maxn]={0},weight[maxn];
    unordered_map<string,int> pos1;
    unordered_map<int,string> pos2;
    unordered_map<string,int> ans;
    struct node {
        string id;
        int total=0;
        int num=0;
    }Node[maxn];
    struct gang {
        string id;
        int num;
    };
    void init () {
        for (int i=0;i<maxn;i++) 
        father[i]=i;
    }
    int findfather (int x) {
        int a=x;
        while (x!=father[x]) 
        x=father[x];
        while (a!=father[a]) {
            int z=a;
            a=father[a];
            father[z]=x;
        }
        return x;
    }
    void Union (int a,int b) {
        int faA=findfather(a);
        int faB=findfather(b);
        if (faA!=faB) {
            if (weight[faA]>weight[faB]) father[faB]=faA;
            else father[faA]=faB;
        }
    }
    bool cmp (gang a,gang b) {
        return a.id<b.id;
    }
    int main () {
        int N,K;
        scanf ("%d %d",&N,&K);
        init ();
        string s1,s2;
        int cnt=1,x;
        vector<pair<string,string>> v1;
        for (int i=0;i<N;i++) {
            cin>>s1>>s2>>x;
            if (pos1[s1]==0) {
                pos1[s1]=cnt;
                pos2[cnt++]=s1;
            }
            if (pos1[s2]==0) {
                pos1[s2]=cnt;
                pos2[cnt++]=s2;
            }
            weight[pos1[s1]]+=x;
            weight[pos1[s2]]+=x;
            v1.push_back({s1,s2});
        }
        for (int i=0;i<v1.size();i++)
        Union (pos1[v1[i].first],pos1[v1[i].second]);
        for (int i=1;i<cnt;i++) {
            Node[findfather(i)].total+=weight[i];
            Node[findfather(i)].num++;
        }
        for (int i=1;i<cnt;i++) {
            if (Node[i].total>K*2&&Node[i].num>2) 
            ans[pos2[i]]=Node[i].num;
        }
        printf ("%d
    ",ans.size());
        vector<gang> vi;
        for (auto it=ans.begin();it!=ans.end();it++)
        vi.push_back({it->first,it->second});
        sort (vi.begin(),vi.end(),cmp);
        for (int i=0;i<vi.size();i++)
        cout<<vi[i].id<<" "<<vi[i].num<<endl;
        return 0;
    }
    View Code

    A1107 Social Clusters(30)

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int maxn=1010;
    int father[maxn];
    int isRoot[maxn];
    int course[maxn];
    int N;
    void init () {
        for (int i=1;i<=N;i++) {
            father[i]=i;
            isRoot[i]=0;
            course[i]=0;
        }
    }
    int findfather (int x) {
        int a=x;
        while (x!=father[x]) 
        x=father[x];
        while (a!=father[a]) {
            int z=a;
            a=father[a];
            father[z]=x;
        }
        return x;
    }
    void Union (int a,int b) {
        int faA=findfather(a);
        int faB=findfather(b);
        if (faA!=faB) father[faA]=faB;
    }
    int main () {
        scanf ("%d",&N);
        init ();
        int k,hobby;
        for (int i=1;i<=N;i++) {
            scanf ("%d: ",&k);
            for (int j=0;j<k;j++) {
                scanf ("%d",&hobby);
                if (course[hobby]==0) course[hobby]=i;
                Union (i,course[hobby]);
            }
        }
        for (int i=1;i<=N;i++)
        isRoot[findfather(i)]++;
        int ans=0;
        for (int i=1;i<=N;i++)
        if (isRoot[i]!=0) ans++;
        printf ("%d
    ",ans);
        sort (isRoot+1,isRoot+N+1);
        for (int i=N;i>=N-ans+1;i--) {
            printf ("%d",isRoot[i]);
            if (i>N-ans+1) printf (" ");
        }
        return 0;
    }
    View Code

    A1114 Family Property(25)

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn=10010;
    struct people {
        int id,fatherid,motherid,num,area;
        int child[10];
    }p[maxn];
    struct family {
        int id,renshu;
        double num,area;
        bool flag=false;
    }adj[maxn];
    bool visit[maxn]={false};
    int father[maxn];
    void init () {
        for (int i=0;i<maxn;i++)
        father[i]=i;
    }
    int findfather (int x) {
        int a=x;
        while (x!=father[x])
        x=father[x];
        while (a!=father[a]) {
            int z=a;
            a=father[a];
            father[z]=x;
        }
        return x;
    }
    void Union (int a,int b) {
        int faA=findfather (a);
        int faB=findfather (b);
        if (faA>faB) father[faA]=faB;
        if (faA<faB) father[faB]=faA;
    }
    bool cmp1 (family a,family b) {
        if (a.area!=b.area) return a.area>b.area;
        else return a.id<b.id;
    }
    int main () {
        int N,cnt=0,k;
        init ();
        scanf ("%d",&N);
        for (int i=0;i<N;i++) {
            scanf ("%d %d %d %d",&p[i].id,&p[i].fatherid,&p[i].motherid,&k);
            visit[p[i].id]=true;
            if (p[i].fatherid!=-1) {
                visit[p[i].fatherid]=true;
                Union (p[i].fatherid,p[i].id);
            }
            if (p[i].motherid!=-1) {
                visit[p[i].motherid]=true;
                Union (p[i].motherid,p[i].id);
            }
            for (int j=0;j<k;j++) {
                scanf ("%d",&p[i].child[j]);
                visit[p[i].child[j]]=true;
                Union (p[i].id,p[i].child[j]);
            }
            scanf ("%d %d",&p[i].num,&p[i].area);
        }
        for (int i=0;i<N;i++) {
            int id=findfather(p[i].id);
            adj[id].id=id;
            adj[id].num+=p[i].num;
            adj[id].area+=p[i].area;
            adj[id].flag=true;
        }
        for (int i=0;i<maxn;i++)
        if (visit[i])
        adj[findfather(i)].renshu++;
        for (int i=0;i<maxn;i++)
        if (adj[i].flag) {
            cnt++;
            adj[i].area=(double)(adj[i].area*1.0/adj[i].renshu);
            adj[i].num=(double)(adj[i].num*1.0/adj[i].renshu);
        }
        printf ("%d
    ",cnt);
        sort (adj,adj+maxn,cmp1);
        for (int i=0;i<cnt;i++)
        printf ("%04d %d %.3f %.3f
    ",adj[i].id,adj[i].renshu,adj[i].num,adj[i].area);
        return 0;
        
    }
    View Code

    A1118 Birds in Forest(25)

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int maxn=100010;
    int father[maxn];
    int isRoot[maxn]={0};
    int visit[maxn]={0};
    void init () {
        for (int i=1;i<maxn;i++)
        father[i]=i;
    }
    int findfather (int x) {
        int a=x;
        while (x!=father[x])
        x=father[x];
        while (a!=father[a]) {
            int z=a;
            a=father[a];
            father[z]=x;
        }
        return x;
    }
    void Union (int a,int b) {
        int faA=findfather(a);
        int faB=findfather(b);
        father[faA]=faB;
    }
    int main () {
        int N;
        scanf ("%d",&N);
        int maxnum=-1;
        int K,x,p;
        init ();
        for (int i=0;i<N;i++) {
            scanf ("%d %d",&K,&x);
            visit[x]++;
            if (x>maxnum) maxnum=x;
            for (int j=1;j<K;j++) {
                scanf ("%d",&p);
                if (p>maxnum) maxnum=p;
                Union (p,findfather(x));
            }
        }
        vector<int> vi;
        int q,u,v;
        scanf ("%d",&q);
        for (int i=0;i<q;i++) {
            scanf ("%d %d",&u,&v);
            if (findfather(u)!=findfather(v)) vi.push_back(0);
            else vi.push_back(1);
        }
        int ans=0;
        for (int i=1;i<=maxnum;i++)
        isRoot[findfather(i)]++;
        for (int i=1;i<=maxnum;i++)
        if (isRoot[i]!=0) ans++;
        printf ("%d %d
    ",ans,maxnum);
        for (int i=0;i<vi.size();i++) {
            if (vi[i]==0) printf ("No
    ");
            else printf ("Yes
    ");
        }
        return 0;
    }
    View Code

    A1119 Pre-and Post-order Traversals(30)

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int maxn=1010;
    int pre[maxn],post[maxn];
    vector<int> in;
    bool unique1=true;
    void inOrder (int preL,int preR,int postL,int postR) {
        if (preL==preR) {
            in.push_back(pre[preL]);
            return;
        }
        if (pre[preL]==post[postR]) {
            int i=preL+1;
            while (i<=preR&&pre[i]!=post[postR-1]) i++;
            if (i-preL>1) 
            inOrder (preL+1,i-1,postL,postL+i-preL-2);
            else unique1=false;
            in.push_back(post[postR]);
            inOrder (i,preR,postL+i-preL-1,postR-1);
        }
    }
    int main () {
        int N;
        scanf ("%d",&N);
        for (int i=0;i<N;i++)
        scanf ("%d",&pre[i]);
        for (int i=0;i<N;i++)
        scanf ("%d",&post[i]);
        inOrder (0,N-1,0,N-1);
        if (unique1==true) printf ("Yes
    ");
        else printf ("No
    ");
        //printf ("%d",in[0]);
        for (int i=0;i<N;i++) {
            if (i!=0) printf (" ");
            printf ("%d",in[i]);
        }
        printf ("
    ");
        //printf ("%d",in[i]);
        return 0;
    } 
    View Code

    专题十一  平方探测法

    A1078 Hashing(25)

    #include<iostream>
    using namespace std;
    int size,n,hashTable[10010];
    bool isprim (int num) {
        if (num==1) return false;
        for (int i=2;i*i<=num;i++)
        if (num%i==0) return false;
        return true;
    } 
    void insert (int key) {
        for (int step=0;step<size;step++) {
            int index1=(key+step*step)%size;
            if (hashTable[index1]==0) {
                hashTable[index1]=1;
                printf ("%d",index1%size);
                return;
            }
        }
        printf ("-");
    }
    int main () {
        scanf ("%d %d",&size,&n);
        while (!isprim(size)) size++;
        for (int i=0;i<n;i++) {
            int key;
            scanf ("%d",&key);
            if (i!=0) printf (" ");
            insert (key);
        }
        return 0;
    }
    View Code

    A1145 Hashing-AST(25)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    bool isprime (int n) {
        for (int i=2;i*i<=n;i++)
        if (n%i==0) return false;
        return true;
    }
    int main () {
        int tsize,n,m,a;
        scanf ("%d %d %d",&tsize,&n,&m);
        while (!isprime(tsize)) tsize++;
        vector<int> v(tsize);
        for (int i=0;i<n;i++) {
            scanf ("%d",&a);
            int flag=0;
            for (int j=0;j<tsize;j++) {
                int pos=(a+j*j)%tsize;
                if (v[pos]==0) {
                    v[pos]=a;
                    flag=1;
                    break;
                }
            }
            if (flag==0) printf ("%d cannot be inserted.
    ",a);
        }
            int ans=0;
            for (int i=0;i<m;i++) {
                scanf ("%d",&a);
                for (int j=0;j<=tsize;j++) {
                    ans++;
                    int pos=(a+j*j)%tsize;
                    if (v[pos]==a||v[pos]==0) break;
                }
            }
           printf ("%.1f
    ",ans*1.0/m);
           return 0;
       }
    View Code

    专题十二  模拟+杂题

    A1002 A+B for Polynomials(25)

    #include<cstdio>
    int main () {
        double num[1005]={0};
        int K,front;
        double behind;
        scanf ("%d",&K);
        for (int i=0;i<K;i++) {
            scanf ("%d %lf",&front,&behind);
            num[front]+=behind;
        }
        scanf ("%d",&K);
        for (int i=0;i<K;i++) {
            scanf ("%d %lf",&front,&behind);
            num[front]+=behind;
        }
        int ans=0;
        for (int i=1004;i>=0;i--)
        if (num[i]!=0) ans++;
        printf ("%d",ans);
        for (int i=1004;i>=0;i--) 
        if (num[i]!=0) printf (" %d %.1f",i,num[i]);
        return 0;
    }
    View Code

    A1009 Product of Polynomials(25)

    #include<cstdio>
    int main () {
        double product[2005]={0};
        double a1[1005]={0},a2[1005]={0};
        int n1,q;
        scanf ("%d",&n1);
        for (int i=0;i<n1;i++) {
            scanf ("%d",&q);
            scanf ("%lf",&a1[q]);
        }
        int n2;
        scanf ("%d",&n2);
        for (int i=0;i<n2;i++) {
            scanf ("%d",&q);
            scanf ("%lf",&a2[q]);
        }
        for (int i=0;i<=1000;i++) {
            for (int j=0;j<=1000;j++) {
                product[i+j]+=a1[i]*a2[j];
            }
        }
        int ans=0;
        for (int i=0;i<=2000;i++) 
        if (product[i]!=0.0) ans++;
        printf ("%d",ans);
        for (int i=2000;i>=0;i--) 
        if (product[i]!=0.0) printf (" %d %.1f",i,product[i]);
        return 0;
    }
    View Code

    A1011 World Cup Betting(20)

    #include<cstdio>
    int main (){
      double w[3],t[3],l[3],a[3],r;
      for (int i=0;i<3;i++)
      scanf ("%lf %lf %lf",&w[i],&t[i],&l[i]);
      for (int i=0;i<3;i++){
        if (w[i]>t[i]&&w[i]>l[i]) {
          printf ("W ");
          a[i]=w[i];
        }
        if (t[i]>w[i]&&t[i]>l[i]) {
          printf ("T ");
          a[i]=t[i];
        }
        if (l[i]>t[i]&&l[i]>w[i]) {
          printf ("L ");
          a[i]=l[i];
        }
      }
      r=(a[0]*a[1]*a[2]*0.65-1)*2;
      printf ("%.2f",r);
      return 0;
    }
      
      
    View Code

    A1014 Waiting in Line(30)

    #include<cstdio>
    #include<queue>
    #include<algorithm>
    using namespace std;
    const int maxNode=1111;
    int n,m,k,query,q;
    int convertToMinute (int h,int m) {
        return h*60+m;
    }
    struct Window {
        int endTime,popTime;
        queue<int> q;
    }window[20];
    int ans[maxNode],needTime[maxNode];
    int main () {
        int inIndex=0;
        scanf ("%d%d%d%d",&n,&m,&k,&query);
        for (int i=0;i<k;i++)
        scanf ("%d",&needTime[i]);
        for (int i=0;i<n;i++)
        window[i].popTime=window[i].endTime=convertToMinute(8,0);
        for (int i=0;i<min(n*m,k);i++) {
            window[inIndex%n].q.push(inIndex);
            window[inIndex%n].endTime+=needTime[inIndex];
            if (inIndex<n) window[inIndex].popTime=needTime[inIndex];
            ans[inIndex]=window[inIndex%n].endTime;
            inIndex++;
        }
        for (;inIndex<k;inIndex++) {
            int idx=-1,minPopTime=1<<30;
            for (int i=0;i<n;i++) {
                if (window[i].popTime<minPopTime) {
                    idx=i;
                    minPopTime=window[i].popTime;
                }
            }
            Window& W=window[idx];
            W.q.pop();
            W.q.push(inIndex);
            W.endTime+=needTime[inIndex];
            W.popTime+=needTime[W.q.front()];
            ans[inIndex]=W.endTime;
        } 
        for (int i=0;i<query;i++) {
            scanf ("%d",&q);
            if (ans[q-1]-needTime[q-1]>=convertToMinute(17,0))
            printf ("Sorry
    ");
            else 
            printf ("%02d:%02d
    ",ans[q-1]/60,ans[q-1]%60);
        }
        return 0;
    }
    View Code

    A1015 Reversible Primes(20)

    #include<cstdio>
    #include<cmath>
    bool isPrime (int n) {
        if (n<=1) return false;
        int sqr=(int)sqrt(1.0*n);
        for (int i=2;i<=sqr;i++) 
        if (n%i==0) return false;
        return true;
    }
    int d[111];
    int main () {
        int n,radix;
        while (scanf ("%d",&n)!=EOF) {
            if (n<0) break;
            scanf ("%d",&radix);
            if (isPrime(n)==false)
            printf ("No
    ");
            else {
                int len=0;
                do {
                    d[len++]=n%radix;
                    n/=radix;
                }while (n!=0);
                for (int i=0;i<len;i++)
                n=n*radix+d[i];
                if (isPrime(n)==true) printf ("Yes
    ");
                else printf ("No
    ");
            }
        }
        return 0;
    }
    View Code

    A1017 Queueing at  Bank(25)

    #include<cstdio>
    #include<vector>
    #include<algorithm>
    using namespace std;
    const int K=111;
    const int INF=1000000000;
    struct Customer {
        int comeTime,serveTime;
    }newCustomer;
    vector<Customer> custom;
    int convertTime (int h,int m,int s) {
        return h*3600+m*60+s;
    }
    bool cmp (Customer a,Customer b) {
        return a.comeTime<b.comeTime;
    }
    int endTime[K];
    int main () {
        int c,w,totTime=0;
        int stTime=convertTime(8,0,0);
        int edTime=convertTime(17,0,0);
        scanf ("%d%d",&c,&w);
        for (int i=0;i<w;i++) endTime[i]=stTime;
        for (int i=0;i<c;i++) {
            int h,m,s,serveTime;
            scanf ("%d:%d:%d %d",&h,&m,&s,&serveTime);
            int comeTime=convertTime(h,m,s);
            if (comeTime>edTime) continue;
            newCustomer.comeTime=comeTime;
            newCustomer.serveTime=serveTime<=60?serveTime*60:3600;
            custom.push_back(newCustomer);
        }
        sort(custom.begin(),custom.end(),cmp);
        for (int i=0;i<custom.size();i++) {
            int idx=-1,minEndTime=INF;
            for (int j=0;j<w;j++) {
                if (endTime[j]<minEndTime) {
                    minEndTime=endTime[j];
                    idx=j;
                }
            }
            if (endTime[idx]<=custom[i].comeTime) {
                endTime[idx]=custom[i].comeTime+custom[i].serveTime;
            }
            else {
                totTime+=(endTime[idx]-custom[i].comeTime);
                endTime[idx]+=custom[i].serveTime;
            }
        }
        if (custom.size()==0) printf ("0.0");
        else printf ("%.1f",totTime/60.0/custom.size());
        return 0;
    }
    View Code

    A1019 General Palindromic Number(20)

    #include<cstdio>
    bool Judge (int z[],int num) {
        for (int i=0;i<=num/2;i++) {
            if (z[i]!=z[num-1-i])
            return false;
        }
        return true;
    }
    int main () {
        int n,b,z[40],num=0;
        scanf ("%d%d",&n,&b);
        do {
            z[num++]=n%b;
            n/=b;
        } while (n!=0);
        bool flag=Judge(z,num);
        if (flag==true) printf ("Yes
    ");
        else printf ("No
    ");
        for (int i=num-1;i>=0;i--) {
            printf ("%d",z[i]);
            if (i!=0) printf (" ");
        }
        return 0;
    }
    View Code

    A1025 PAT Ranking(25)

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    struct student {
        char id[15];
        int score;
        int location_number;
        int local_rank;
    }stu[30010];
    bool cmp (student a,student b) {
        if (a.score!=b.score) return a.score>b.score;
        else return strcmp(a.id,b.id)<0;
    } 
    int main () {
        int n,k,num=0;
        scanf ("%d",&n);
        for (int i=1;i<=n;i++) {
            scanf ("%d",&k);
            for (int j=0;j<k;j++) {
                scanf ("%s %d",stu[num].id,&stu[num].score);
                stu[num].location_number=i;
                num++;
            }
            sort (stu+num-k,stu+num,cmp);
            stu[num-k].local_rank=1;
            for (int j=num-k+1;j<num;j++) {
                if (stu[j].score==stu[j-1].score) {
                    stu[j].local_rank=stu[j-1].local_rank;
                }
                else {
                    stu[j].local_rank=j+1-(num-k);
                }
            }
        }
        printf ("%d
    ",num);
        sort (stu,stu+num,cmp);
        int r=1;
        for (int i=0;i<num;i++) {
            if (i>0&&stu[i].score!=stu[i-1].score) {
                r=i+1;
            }
            printf ("%s ",stu[i].id);
            printf ("%d %d %d
    ",r,stu[i].location_number,stu[i].local_rank);
        }
        return 0;
    }
    View Code

    A1027 Colors in Mars(20)

    #include<cstdio> 
    char radix[13]={'0','1','2','3','4','5','6','7','8','9','A','B','C'};
    int main () {
        int r,g,b;
        scanf ("%d%d%d",&r,&g,&b);
        printf ("#");
        printf ("%c%c",radix[r/13],radix[r%13]);
        printf ("%c%c",radix[g/13],radix[g%13]);
        printf ("%c%c",radix[b/13],radix[b%13]);
        return 0;
    }
    View Code

    A1029 Medians(25)

    #include<cstdio>
    #include<vector>
    #include<algorithm>
    using namespace std;
    int main () {
        vector<int> vi;
        int n;
        scanf ("%d",&n);
        for (int i=0;i<n;i++) {
            int w;
            scanf ("%d",&w);
            vi.push_back(w);
        }
        int m;
        scanf ("%d",&m);
        for (int i=0;i<m;i++) {
            int j;
            scanf ("%d",&j);
            vi.push_back(j);
        }
        sort (vi.begin(),vi.begin()+vi.size());
        if (vi.size()%2==0) printf ("%d
    ",vi[vi.size()/2-1]);
        else printf ("%d
    ",vi[vi.size()/2]);
        return 0;
    }
    View Code

    A1031 Hello World for U(20)

    #include<cstdio>
    #include<cstring>
    int main () {
        char s[81];
        int w=0;
        while ((s[w]=getchar())!='
    ')
        w++;
        s[w]='';
        int N=strlen(s);
        int n1=(N+2)/3;
        int n3=n1;
        int n2=N+2-n1*2;
        for (int i=0;i<n1-1;i++) {
            printf ("%c",s[i]);
            for (int j=0;j<n2-2;j++)
            printf (" ");
            printf ("%c
    ",s[N-i-1]);
        }
        for (int i=0;i<n2;i++) 
        printf ("%c",s[n1+i-1]);
        return 0;
    } 
    View Code

    A1032 Sharing(25)

    #include<cstdio>
    using namespace std;
    struct node {
        char key;
        int next;
        bool flag;
    }node[100014];
    int main () {
        int s1,s2,n,a,b;
        scanf ("%d%d%d",&s1,&s2,&n);
        char data;
        for (int i=0;i<n;i++) {
            scanf ("%d %c %d",&a,&data,&b);
            node[a]={data,b,false};
        }
        for (int i=s1;i!=-1;i=node[i].next)
        node[i].flag=true;
        for (int i=s2;i!=-1;i=node[i].next)
        if (node[i].flag==true) {
        printf ("%05d",i);
        return 0;
        }
        printf ("-1");
        return 0;
    } 
    View Code

    A1033 To Fill or Not to Fill

    #include<bits/stdc++.h>
    using namespace std;
    const int inf=1e9;
    struct station {
        double price,dis;
    };
    bool cmp (station a,station b) {
        return a.dis<b.dis;
    }
    int main () {
        double cmax,d,davg;
        int n;
        scanf ("%lf%lf%lf%d",&cmax,&d,&davg,&n);
        vector<station> sta(n+1);
        sta[0]={0.0,d};
        for (int i=1;i<=n;i++) {
            scanf ("%lf%lf",&sta[i].price,&sta[i].dis);
        }
        sort (sta.begin(),sta.end(),cmp);
        double nowdis=0.0,maxdis=0.0,nowprice=0.0,totalPrice=0.0,leftdis=0.0;
        if (sta[0].dis!=0) {
            printf ("The maximum travel distance = 0.00");
            return 0;
        }
        else nowprice=sta[0].price;
        while (nowdis<d) {
            maxdis=nowdis+cmax*davg;
            double minPriceDis=0,minPrice=inf;
            int flag=0;
            for(int i=1;i<=n&&sta[i].dis<=maxdis;i++) {
                if (sta[i].dis<=nowdis) continue;
                if (sta[i].price<nowprice) {
                    totalPrice+=(sta[i].dis-nowdis-leftdis)*nowprice/davg;
                    leftdis=0.0;
                    nowprice=sta[i].price;
                    nowdis=sta[i].dis;
                    flag=1;
                    break;
                }
                if(sta[i].price<minPrice) {
                minPrice=sta[i].price;
                minPriceDis=sta[i].dis;
                }
            }
            if(flag==0&&minPrice!=inf) {
                totalPrice+=(nowprice*(cmax-leftdis/davg));
                leftdis=cmax*davg-(minPriceDis-nowdis);
                nowprice=minPrice;
                nowdis=minPriceDis; 
            }
            if(flag == 0 && minPrice == inf) {
                nowdis+=cmax*davg;
                printf("The maximum travel distance = %.2f",nowdis);
                return 0;
            }
        }
        printf("%.2f",totalPrice);
        return 0; 
    }
    View Code

    A1037 Magic Coupon(25)

    #include<cstdio>
    #include<vector>
    #include<algorithm>
    using namespace std;
    bool cmp (int a,int b) {
        return a>b;
    }
    int main () {
        vector<int> pos1,mns1,pos2,mns2;
        int a[100010],b[100010];
        int Na,Nb;
        scanf ("%d",&Na);
        for (int i=0;i<Na;i++) {
            scanf ("%d",&a[i]);
            if (a[i]>0) pos1.push_back(a[i]);
            if (a[i]<0) mns1.push_back(a[i]);
        }
        scanf ("%d",&Nb);
        for (int i=0;i<Nb;i++) {
            scanf ("%d",&b[i]);
            if (b[i]>0) pos2.push_back(b[i]);
            if (b[i]<0) mns2.push_back(b[i]);
        }
        sort (pos1.begin(),pos1.begin()+pos1.size(),cmp);
        sort (pos2.begin(),pos2.begin()+pos2.size(),cmp);
        sort (mns1.begin(),mns1.begin()+mns1.size());
        sort (mns2.begin(),mns2.begin()+mns2.size());
        int len1=min(pos1.size(),pos2.size());
        int len2=min(mns1.size(),mns2.size());
        int sum=0;
        for (int i=0;i<len1;i++)
        sum+=pos1[i]*pos2[i];
        for (int i=0;i<len2;i++)
        sum+=mns1[i]*mns2[i];
        printf ("%d",sum);
        return 0;
        
    }
    
     
    View Code

    A1038 Recover the Smallest Number(30)

    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    bool cmp (string a,string b) {
        return a+b<b+a;
    }
    int main () {
        string a[10005];
        int N;
        scanf ("%d",&N);
        
        for (int i=0;i<N;i++)
        cin>>a[i];
        sort(a,a+N,cmp);
        string ans;
        for (int i=0;i<N;i++)
        ans+=a[i];
        while (ans.size()!=0&&ans[0]=='0')
        ans.erase(ans.begin());
        if (ans.size()==0) cout<< 0;
        else cout<< ans;
        return 0;
    }
    View Code

    A1039 Course Lisr for Student(25)

    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    using namespace std;
    const int N=40010;
    const int M=26*26*26*10+1;
    vector<int> selectCourse[M];
    int getID (char name[]) {
        int id=0;
        for (int i=0;i<3;i++) 
        id=id*26+(name[i]-'A');
        id=id*10+(name[3]-'0');
        return id;
    }
    int main () {
        char name[5];
        int n,k;
        scanf ("%d%d",&n,&k);
        for (int i=0;i<k;i++) {
            int course,x;
            scanf ("%d%d",&course,&x);
            for (int j=0;j<x;j++) {
                scanf ("%s",name);
                int id=getID(name);
                selectCourse[id].push_back(course);
            }
        }
        for (int i=0;i<n;i++) {
            scanf ("%s",name);
            int id=getID(name);
            sort(selectCourse[id].begin(),selectCourse[id].end());
            printf ("%s %d",name,selectCourse[id].size());
            for (int j=0;j<selectCourse[id].size();j++) 
            printf (" %d",selectCourse[id][j]);
            printf ("
    ");
        }
        return 0;
    }
    View Code

    A1040 Longest Symmetric String(25)

    #include<cstdio>
    #include<cstring>
    #include<vector>
    using namespace std;
    int main () {
        char s[1005];
        int w=0;
        while ((s[w]=getchar())!='
    ')
        w++;
        s[w]='';
        int maxlen=0;
        int len;
        vector<char> t1,t2;
        for (int i=0;i<w;i++) {
            for (int j=w-1;j>=0;j--) {
                if (s[i]==s[j]) {
                    int l=i,r=j;
                    int flag=0;
                    while (l<r) {
                        l++;r--;
                        if (s[l]!=s[r]) {
                            flag++;break;
                        }
                    }
                    if (flag) continue;
                    len=j-i+1;
                    if (len>maxlen) maxlen=len;
                }
            }
        }
        printf ("%d",maxlen);
        return 0;
    }
    View Code

    A1041 Be Unique(20)

    #include<cstdio>
    using namespace std;
    int main () {
        int HashTable[10005]={0};
        int N;
        scanf ("%d",&N);
        int a[100005],i;
        for (i=0;i<N;i++) {
        scanf ("%d",&a[i]);
        HashTable[a[i]]++;
    }
    int ans=-1;
        for (i=0;i<N;i++)
        if (HashTable[a[i]]==1) {
            ans=a[i];
        break;}
        if (ans==-1) printf ("None");
        else printf ("%d",ans);
        return 0;
        
    }
    View Code

    A1042 Shuffling Machine(20)

    #include<cstdio>
    char hs[5]={'S','H','C','D','J'};
    int main () {
        int start[55];
        int end[55];
        for (int i=1;i<=54;i++)
        start[i]=i;
        int K;
        scanf ("%d",&K);
        int op[55];
        for (int i=1;i<=54;i++)
        scanf ("%d",&op[i]);
        while (K--) {
            for (int i=1;i<=54;i++)
            end[op[i]]=start[i];
            for (int i=1;i<=54;i++)
            start[i]=end[i];
        }
        for (int i=1;i<=54;i++)
        {
            start[i]--;
            if (i!=54) printf ("%c%d ",hs[start[i]/13],start[i]%13+1);
            else printf ("%c%d",hs[start[i]/13],start[i]%13+1);
        }
        return 0;
    }
    View Code

    A1046 Shortest Distance(20)

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<math.h>
    using namespace std;
    int main ()
    {
        int dis[100005]={0};
        int N,q;
        vector<int> s;
        scanf ("%d",&N);
        dis[1]=0;
        for (int i=1;i<=N;i++) {
            scanf ("%d",&q);
            dis[i+1]=dis[i]+q;
        }
        int T,d1,d2;
        scanf ("%d",&T);
        for (int i=0;i<T;i++) {
            scanf ("%d %d",&d1,&d2);
            int temp=fabs(dis[d1]-dis[d2]);
            s.push_back(min(temp,dis[N+1]-temp));
        }
        for (int i=0;i<s.size();i++)
        printf ("%d
    ",s[i]);
        return 0;    
     } 
    View Code

    A1047 Student List for Course(25)

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    char name[40010][5];
    vector<int> course[2510];
    bool cmp1(int a, int b) {
        return strcmp(name[a], name[b]) < 0;
    }
    int main() {
        int n, k;
        scanf("%d %d", &n, &k);
        for(int i = 0; i < n; i++) {
            int cnt, temp;
            scanf("%s %d", name[i], &cnt);
            for(int j = 0; j < cnt; j++) {
                scanf("%d", &temp);
                course[temp].push_back(i);
            }
        }
        for(int i = 1; i <= k; i++) {
            printf("%d %d
    ", i, course[i].size());
            sort(course[i].begin(), course[i].end(), cmp1);
            for(int j = 0; j < course[i].size(); j++)
                printf("%s
    ", name[course[i][j]]);
        }
        return 0;
    }
    View Code

    A1048 Find Coins(25)

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int N=1005;
    int HashTable[N]={0};
    int main () {
        int n,m,a;
        scanf ("%d%d",&n,&m);
        for (int i=0;i<n;i++) {
            scanf ("%d",&a);
            ++HashTable[a];
        }
        for (int i=1;i<m;i++) {
            if (HashTable[i]&&HashTable[m-i]) {
                if (i==m-i&&HashTable[i]<=1) continue;
                printf ("%d %d
    ",i,m-i);
                return 0;
            }
        }
            printf ("No Solution
    ");
            return 0;
        
    }
    View Code

    A1049 Counting Ones(30)

    #include <iostream>
    using namespace std;
    int main() {
        int n, left = 0, right = 0, a = 1, now = 1, ans = 0;
        scanf("%d", &n);
        while(n / a) {
            left = n / (a * 10), now = n / a % 10, right = n % a;
            if(now == 0) ans += left * a;
            else if(now == 1) ans += left * a + right + 1;
            else ans += (left + 1) * a;
            a = a * 10;
        }
        printf("%d", ans);
        return 0;
    }
    View Code

    A1050 String Substractoin(20)

    #include<cstdio>
    #include<cstring>
    using namespace std;
    int main () {
        bool HashTable[128]={false};
        char s1[10005];
        int w=0;
        while ((s1[w]=getchar())!='
    ')
        w++;
        s1[w]='';
        char c;
        while ((c=getchar())!='
    ') 
        HashTable[c]=true;
        for (int i=0;i<strlen(s1);i++)
        if (HashTable[s1[i]]==false)
        printf ("%c",s1[i]);
        return 0;
    }
    View Code

    A1051 Pop Sequence(25)

    #include<cstdio>
    #include<stack>
    using namespace std;
    const int maxn=1010;
    int arr[maxn];
    stack<int> st;
    int main () {
        int m,n,T;
        scanf ("%d%d%d",&m,&n,&T);
        while (T--) {
            while (!st.empty()) st.pop();
        for (int i=1;i<=n;i++) 
        scanf ("%d",&arr[i]);
        int current=1;
        bool flag=true;
        for (int i=1;i<=n;i++) {
            st.push(i);
            if (st.size()>m) {
                flag=false;
                break;
            }
            while (!st.empty()&&st.top()==arr[current]) {
                st.pop();
                current++;
            }
        }
        if (st.empty()==true&&flag==true)
        printf ("YES
    ");
        else printf ("NO
    ");
    }
    return 0;
    }
    View Code

    A1052 Linked List Sorting(25)

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn=100005;
    struct Node {
        int address,data,next;
        bool flag;
    }node[maxn];
    bool cmp (Node a,Node b) {
        if (a.flag==false||b.flag==false) 
        return a.flag>b.flag;
        else return a.data<b.data;
    } 
    int main () {
        for (int i=0;i<maxn;i++) 
        node[i].flag=false;
        int n,begin,address;
        scanf ("%d %d",&n,&begin);
        for (int i=0;i<n;i++) {
            scanf ("%d",&address);
            scanf ("%d %d",&node[address].data,&node[address].next);
            node[address].address=address;
        }
        int cnt=0,p=begin;
        while (p!=-1) {
            node[p].flag=true;
            cnt++;
            p=node[p].next;
    }
        if (cnt==0) printf ("0 -1");
        else {
            sort (node,node+maxn,cmp);
            printf ("%d %05d
    ",cnt,node[0].address);
            for (int i=0;i<cnt;i++){
                if (i!=cnt-1) printf ("%05d %d %05d
    ",node[i].address,node[i].data,node[i+1].address);
                else printf ("%05d %d -1
    ",node[i].address,node[i].data);
            }
        }    
        return 0;
    }
    View Code

    A1055 The World Richest(25)

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <cstring>
    using namespace std;
    struct node {
        char name[10];
        int age, money;
    };
    int cmp1(node a, node b) {
        if(a.money != b.money)
            return a.money > b.money;
        else if(a.age != b.age)
            return a.age < b.age;
        else
            return (strcmp(a.name, b.name) < 0);
    }
    
    int main() {
        int n, k, num, amin, amax;
        scanf("%d %d", &n, &k);
        vector<node> vt(n), v;
        vector<int> book(205, 0);
        for(int i = 0; i < n; i++)
            scanf("%s %d %d", vt[i].name, &vt[i].age, &vt[i].money);
        sort(vt.begin(), vt.end(), cmp1);
        for(int i = 0; i < n; i++) {
            if(book[vt[i].age] < 100) {
                v.push_back(vt[i]);
                book[vt[i].age]++;
            }
        }
        for(int i = 0; i < k; i++) {
            scanf("%d %d %d", &num, &amin, &amax);
            vector<node> t;
            for(int j = 0; j < v.size(); j++) {
                if(v[j].age >= amin && v[j].age <= amax)
                    t.push_back(v[j]);
            }
            if(i != 0) printf("
    ");
            printf("Case #%d:", i + 1);
            int flag = 0;
            for(int j = 0; j < num && j < t.size(); j++) {
                printf("
    %s %d %d", t[j].name, t[j].age, t[j].money);
                flag = 1;
            }
            if(flag == 0) printf("
    None");
        }
        return 0;
    }
    View Code

    A1056 Mice and Rice(25)

    #include<cstdio>
    #include<queue>
    using namespace std;
    const int maxn=1010;
    struct mouse {
        int weight;
        int R;
    }mouse[maxn];
    int main () {
        int np,ng,order;
        scanf ("%d%d",&np,&ng);
        for (int i=0;i<np;i++)
        scanf ("%d",&mouse[i].weight);
        queue<int> q;
        for (int i=0;i<np;i++) {
        scanf ("%d",&order);
        q.push(order);
        }
        int temp=np,group;
        while (q.size()!=1) {
            if (temp%ng==0) group=temp/ng;
            else group=temp/ng+1;
            for (int i=0;i<group;i++) {
                int k=q.front();
                for (int j=0;j<ng;j++) {
                    if (i*ng+j>=temp) break;
                    int front=q.front();
                    if (mouse[front].weight>mouse[k].weight)
                    k=front;
                    mouse[front].R=group+1;
                    q.pop();
                }
                q.push(k);
             }
             temp=group;
        }
        mouse[q.front()].R=1;
        for (int i=0;i<np;i++) {
          printf ("%d",mouse[i].R);
          if (i<np-1) printf (" ");
        } 
        return 0;
    }
    View Code

    A1058 A+B in Hogwarts(20)

    #include<cstdio>
    int main () {
        int x1,x2,x3,y1,y2,y3,z1=0,z2=0,z3=0;
        scanf ("%d.%d.%d %d.%d.%d",&x1,&x2,&x3,&y1,&y2,&y3);
        z3=x3+y3;z2=x2+y2;z1=x1+y1;
        if (z3>=29) {
            z3-=29;
            z2+=1;
        }
        if (z2>=17) {
            z2-=17;
            z1+=1;
        }
        printf ("%d.%d.%d",z1,z2,z3);
        return 0;        
    } 
    View Code

    A1059 Prime Factors(25)

    #include<cstdio>
    #include<cmath>
    const int maxn=100010;
    bool is_prime(int n) {
        if (n==1) return false;
        int sqr=(int)sqrt(1.0*n);
        for (int i=2;i<=sqr;i++)
        if (n%i==0) return false;
        return true;
    }
    int prime[maxn],pNum=0;
    void Find_prime () {
        for (int i=1;i<maxn;i++)
        if (is_prime(i)==true)
        prime [pNum++]=i;
    }
    struct Factor {
        int x,cnt;
    }fac[10];
    int main () {
        Find_prime ();
        int n,num=0;
        scanf ("%d",&n);
        if (n==1) printf ("1=1");
        else {
            printf ("%d=",n);
            int sqr=(int)sqrt(1.0*n);
            for (int i=0;i<pNum&&prime[i]<=sqr;i++) {
                if (n%prime[i]==0) {
                    fac[num].x=prime[i];
                    fac[num].cnt=0;
                    while (n%prime[i]==0) {
                        fac[num].cnt++;
                        n/=prime[i];
                    }
                    num++;
                }
                if (n==1) break;
            }
            if (n!=1) {
                fac[num].x=n;
                fac[num++].cnt=1;
            }
            for (int i=0;i<num;i++) {
                if (i>0) printf ("*");
                printf ("%d",fac[i].x);
                if (fac[i].cnt>1)
                printf ("^%d",fac[i].cnt);
            }
        }
        return 0;
    }
    View Code

    A1062 Talent and Virture(25)

    #include<algorithm>
    #include<iostream>
    #include<vector>
    using namespace std;
    struct people{
        int talent;
        int virtue;
        int id;
        int flag;
    };
    bool cmp(people a,people b){
        if(a.flag==b.flag){
            if((a.virtue+a.talent)==(b.virtue+b.talent)){
                if(a.virtue==b.virtue){
                    return a.id<b.id;
                }else{
                    return a.virtue>b.virtue;
                }
            }else{
                return a.virtue+a.talent>b.virtue+b.talent;
            }
        }else{
            return a.flag<b.flag;
        }
    }
    
    int main(){
        int N,L,H;
        while(scanf("%d%d%d",&N,&L,&H)!=EOF){
            vector<people> vp;
            people input;
            for(int i=0;i<N;i++){
                scanf("%d%d%d",&input.id,&input.virtue,&input.talent);
                if(input.virtue>=H&&input.talent>=H){// sages
                    input.flag = 1;
                }else if((input.talent<H&&input.talent>=L)&&input.virtue>=H){// noblemen
                    input.flag = 2;
                }else if((input.virtue<H&&input.virtue>=L) && 
                    (input.talent<H&&input.talent>=L) && 
                    (input.virtue>=input.talent)){// foolmen
                    input.flag = 3;
                }else if(input.virtue>=L&&input.talent>=L){// the rest
                    input.flag = 4;
                }else{
                    continue;
                }
                vp.push_back(input);
            }        
            sort(vp.begin(),vp.end(),cmp);
            printf("%d
    ",vp.size());
            for(auto elem:vp){
                printf("%08d %d %d
    ",elem.id,elem.virtue,elem.talent);
            }
        }
        return 0;
    }
    View Code

    A1065 A+B and C(20)

    #include <cstdio>
    using namespace std;
    int main() {
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            long long a, b, c;
            scanf("%lld %lld %lld", &a, &b, &c);
            long long sum = a + b;
            if(a > 0 && b > 0 && sum < 0) {
                printf("Case #%d: true
    ", i + 1);
            } 
            else if (a < 0 && b < 0 && sum >= 0) {
                printf("Case #%d: false
    ", i + 1);
            } 
            else if (sum > c) {
                printf("Case #%d: true
    ", i + 1);
            } 
            else {
                printf("Case #%d: false
    ", i + 1);
            }
        }
        return 0; 
    } 
    View Code

    A1067 Sort with Swap(25)

    #include<bits/stdc++.h>
    using namespace std;
    int main () {
        int n,t,cnt=0,a[100010];
        scanf ("%d",&n);
        for (int i=0;i<n;i++) {
            scanf ("%d",&t);
            a[t]=i;
        }
        for (int i=1;i<n;i++) {
            if (i!=a[i]) {
                while (a[0]!=0) {
                    swap(a[a[0]],a[0]);
                    cnt++;
                }
                if (i!=a[i]) {
                    swap  (a[0],a[i]);
                    cnt++;
                }
            }
        }
        printf ("%d
    ",cnt);
        return 0;
    } 
    View Code

    A1068 Find More Coins(30)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=10010;
    int dp[10010],w[10010];
    bool choice[10010][110];
    int cmp1 (int a,int b) {
        return a>b;
    } 
    int main () {
        int n,m;
        scanf ("%d %d",&n,&m);
        for (int i=1;i<=n;i++)
        scanf ("%d",&w[i]);
        sort (w+1,w+n+1,cmp1);
        for (int i=1;i<=n;i++) {
            for (int j=m;j>=w[i];j--) {
                if (dp[j]<=dp[j-w[i]]+w[i]) {
                    choice[i][j]=true;
                    dp[j]=dp[j-w[i]]+w[i];
                }
            }
        }
        if (dp[m]!=m) printf ("No Solution");
        else {
            vector<int> arr;
            int v=m,index1=n;
            while (v>0) {
                if (choice[index1][v]==true) {
                    arr.push_back(w[index1]);
                    v-=w[index1];
                }
                index1--;
            }
            for (int i=0;i<arr.size();i++) {
                if (i!=0) printf (" ");
                printf ("%d",arr[i]);
            }
        }
        return 0;
    }
    View Code

    A1070 Mooncake(25)

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    struct mooncake {
        double store;
        double sell;
        double price;
    }a[1005];
    bool cmp (mooncake a,mooncake b) {
        return a.price>b.price;
    }
    int main () {
        int N;double D;
        scanf ("%d %lf",&N,&D);
        for (int i=0;i<N;i++)
        scanf ("%lf",&a[i].store);
        for (int i=0;i<N;i++)
        scanf ("%lf",&a[i].sell);
        for (int i=0;i<N;i++)
        a[i].price=a[i].sell/a[i].store;
        sort(a,a+N,cmp);
        double sum=0,profit=0;
        for (int i=0;i<N;i++) {
            if (D-sum>=a[i].store) {
                profit+=a[i].sell;
                sum+=a[i].store;
            } 
            else {
                profit+=a[i].price*(D-sum);
                break;
            }
        }
        printf ("%.2f",profit);
        return 0;
    } 
    View Code

    A1081 Rational Sum(20)

    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    struct fraction {
        ll up,down;
    };
    ll gcd(ll a,ll b) {
        if (b==0) return a;
        else return gcd(b,a%b);
    }
    fraction reduction (fraction result) {
        if (result.down<0) {
            result.up=-result.up;
            result.down=-result.down;
        }
        if (result.up==0) result.down=1;
        else {
            int d=gcd(abs(result.up),abs(result.down));
            result.up/=d;
            result.down/=d;
        }
        return result;
    }
    fraction add (fraction fa,fraction fb) {
        fraction result;
        result.up=fa.up*fb.down+fb.up*fa.down;
        result.down=fa.down*fb.down;
        return reduction(result);
    }
    void showResult (fraction r) {
        //reduction(r);
        if (r.down==1) printf ("%lld",r.up);
        else if (abs(r.up)>r.down)
        printf ("%lld %lld/%lld",r.up/r.down,abs(r.up)%r.down,r.down);
        else printf ("%lld/%lld",r.up,r.down);
    }
    int main () {
        int N;
        scanf ("%d",&N);
        fraction r1,r2,sum;
        sum.up=0;
        sum.down=1;
        for (int i=0;i<N;i++) {
            scanf ("%lld/%lld",&r1.up,&r1.down);
            sum=add(sum,r1);
        }
        showResult(sum);
        return 0;
    }
    View Code

    A1082 Read Number in Chinese(25)

    #include<cstdio>
    #include<cstring>
    char num[10][5]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
    char wei[5][5]={"Shi","Bai","Qian","Wan","Yi"};
    int main () {
        char str[15];
        int w=0;
        while ((str[w]=getchar())!='
    ')
        w++;
        str[w]='';
        int len=strlen(str);
        int left=0,right=len-1;
        if (str[0]=='-') {
            printf ("Fu");
            left++;
        }
        while (left+4<=right)
        right-=4;
        while (left<len) {
            bool flag=false;
            bool isPrint=false;
            while (left<=right) {
                if (left>0&&str[left]=='0')
                flag=true;
                else {
                    if (flag==true) {
                        printf (" ling");
                        flag=false;
                    }
                    if (left>0) printf (" ");
                    printf ("%s",num[str[left]-'0']);
                    isPrint=true;
                    if (left!=right)
                    printf (" %s",wei[right-left-1]);
                }
                left++;
            }
            if (isPrint==true&&right!=len-1)
            printf (" %s",wei[(len-1-right)/4+2]);
            right+=4;
        }
        return 0;
    }
    View Code

    A1083 List Grades(25)

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    struct stu {
        char name[12];
        char id[12];
        int grade;
    };
    int cmp1(stu a, stu b) {
        return a.grade > b.grade;
    }
    int main() {
        int n, low, high, cnt = 0;
        scanf("%d", &n);
        vector<stu> v(n);
        for(int i = 0; i < n; i++) {
            scanf("%s %s %d", v[i].name, v[i].id, &v[i].grade);
        }
        scanf("%d %d", &low, &high);
        for(int i = 0; i < n; i++) {
            if(v[i].grade < low || v[i].grade > high) {
                v[i].grade = -1;
            } else {
                cnt++;
            }
        }
        sort(v.begin(), v.end(), cmp1);
        for(int i = 0; i < cnt; i++) {
            printf("%s %s
    ", v[i].name, v[i].id);
        }
        if(cnt == 0)
            printf("NONE");
        return 0;
    }
    View Code

    A1084 Broken Keyboard(20)

    #include<cstdio>
    #include<cstring>
    using namespace std;
    int main () {
        char s1[85],s2[85];
        int w=0;
        bool HashTable[128]={false};
        while ((s1[w]=getchar())!='
    ')
        w++;
        s1[w]='';
        w=0;
        while ((s2[w]=getchar())!='
    ')
        w++;
        s2[w]='';
        int len1=strlen(s1),len2=strlen(s2);
        int i,j;
        for (i=0;i<len1;i++) {
            for (j=0;j<len2;j++) {
                if (s1[i]>='a'&&s1[i]<='z')
                s1[i]-=32;
                if (s2[j]>='a'&&s2[j]<='z')
                s2[j]-=32;
                if (s1[i]==s2[j]) break;
            }
            if (j==len2&&HashTable[s1[i]]==false) {
                HashTable[s1[i]]=true;
                printf ("%c",s1[i]);
            }
        }
        return 0;
    }
    View Code

    A1085 Perfect Sequence(25)

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int main () {
        int N,P;
        scanf ("%d %d",&N,&P);
        int a[100005];
        for (int i=0;i<N;i++)
        scanf ("%d",&a[i]);
        sort(a,a+N);
        int ans=1,maxans=0;
        for (int i=0;i<N-1;i++) {
            int j=upper_bound(a+i+1,a+N,(long long)a[i]*P)-a;
            ans=max(ans,j-i);
        }
        printf ("%d",ans);
        return 0;
    }
    View Code

    A1092 To Buy or Not To Buy(20)

    #include<cstdio>
    #include<cstring>
    using namespace std;
    int main () {
        char s1[1005],s2[1005];
        int w=0;
        while ((s1[w]=getchar())!='
    ')
        w++;
        s1[w]='';
        w=0;
        while ((s2[w]=getchar())!='
    ')
        w++;
        s2[w]='';
        int len1=strlen(s1);
        int len2=strlen(s2);
        int HashTable1[128]={0},HashTable2[128]={0};
        for (int i=0;i<len1;i++)
        HashTable1[s1[i]]++;
        for (int i=0;i<len2;i++)
        HashTable2[s2[i]]++;
        int flag=0;
        for (int i=0;i<128;i++) {
            if (HashTable1[i]<HashTable2[i]) {
                flag+=HashTable2[i]-HashTable1[i];    
            }
        }
        if (flag==0) {
            printf ("Yes %d",strlen(s1)-strlen(s2));
        }
        else printf ("No %d",flag);
        return 0;
        
    }
    View Code
  • 相关阅读:
    CentOS Linux下VNC Server远程桌面配置详解
    Java 中的悲观锁和乐观锁的实现
    spring @configuration使用
    MySQL 汉字拼音
    chmod用数字来表示权限的方法
    C语言创建删不掉的目录
    Android小经验
    系统清理——查找大文件
    最全Pycharm教程(42)——Pycharm扩展功能之Emacs外部编辑器
    怎样学习程序
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/12350684.html
Copyright © 2020-2023  润新知