天梯赛L2L3题解汇总

L1-049 天梯赛座位分配

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
int g[maxn][maxn];
int c[maxn];
int site[1000010];
int N;
int main () {
    scanf ("%d",&N);
    int sum=0;
    for (int i=1;i<=N;i++) {
        scanf ("%d",&c[i]);
        c[i]*=10;
        sum+=c[i];
    }
    int cnt[maxn]={0};
    int now=1,renshu=1;
    while (renshu<=sum) {
        for (int i=1;i<=N;i++) {
            if (cnt[i]<c[i]) {
                if (site[now-1]!=i) {
                    g[i][cnt[i]++]=now;
                    site[now]=i;
                    now++;
                    renshu++;
                }
                else {
                    now++;
                    g[i][cnt[i]++]=now;
                    site[now]=i;
                    now+=2;
                    renshu++;
                }
            }
        }
    }
    for (int i=1;i<=N;i++) {
        printf ("#%d\n",i);
        for (int j=0;j<c[i];j++) {
            printf ("%d",g[i][j]);
            if (j<c[i]-1) {
                if ((j+1)%10==0) printf ("\n");
                else printf (" ");
            }
            else printf ("\n");
        }
    }
    return 0;
}
View Code

L2-001 紧急救援

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1010;
const int inf=1000000000;
int d[maxn],w[maxn],weight[maxn],num[maxn],pre[maxn];
int g[maxn][maxn];
bool vis[maxn]={false};
int N,M,st,ed;
void dijkstra (int s) {
    for (int i=0;i<N;i++) {
        d[i]=inf;num[i]=0;w[i]=0;
    }
    d[s]=0;num[s]=1;w[s]=weight[s];
    for (int i=0;i<N;i++) {
        int u=-1,min=inf;
        for (int j=0;j<N;j++) 
        if (d[j]<min&&vis[j]==false) {
            u=j;min=d[j];
        }
        if (u==-1) return;
        vis[u]=true;
        for (int v=0;v<N;v++) {
            if (vis[v]==false&&g[u][v]!=inf) {
                if (d[u]+g[u][v]<d[v]) {
                    pre[v]=u;
                    num[v]=num[u];
                    w[v]=w[u]+weight[v];
                    d[v]=d[u]+g[u][v];
                }
                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;
                    }
                }
            }
        }
    }
}
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);
    for (int i=0;i<N;i++)
    scanf ("%d",&weight[i]);
    int u,v;
    for (int i=0;i<N;i++)
    for (int j=0;j<N;j++)
    g[i][j]=inf;
    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\n",num[ed],w[ed]);
    DFS (ed);
    return 0;
}
View Code

L2-002 链表去重

#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=100010;
struct node {
    int data;
    int next;
}Node[maxn];
int course1[maxn]={0};
int main () {
    int N,head1,num;
    scanf ("%d %d",&head1,&N);
    for (int i=0;i<N;i++) {
        scanf ("%d",&num);
        scanf ("%d %d",&Node[num].data,&Node[num].next);
    }
    vector<int> la,lb;
    while (head1!=-1) {
        int tmp=abs(Node[head1].data);
             if (course1[tmp]==0) {
                 course1[tmp]=1;
                 la.push_back(head1);
             }
             else lb.push_back(head1);
         head1=Node[head1].next;
    }
    for (int i=0;i<la.size();i++) {
        if (i<la.size()-1) 
        printf ("%05d %d %05d\n",la[i],Node[la[i]].data,la[i+1]);
        else {
            printf ("%05d %d -1\n",la[i],Node[la[i]].data);
        }
    }
    for (int i=0;i<lb.size();i++) {
        if (i<lb.size()-1) 
        printf ("%05d %d %05d\n",lb[i],Node[lb[i]].data,lb[i+1]);
        else {
            printf ("%05d %d -1\n",lb[i],Node[lb[i]].data);
        }
    }
    return 0;
}
View Code

L2-004 这是二叉搜索树吗?

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node {
    int data;
    node * left;node * right;
};
void create (node * &root,int data) {
    if (root==NULL) {
        root=new node;
        root->data=data;
        root->left=NULL;
        root->right=NULL;
        return;
    }
    if (data<root->data) create(root->left,data);
    else create(root->right,data);
}
void preOrder (node * root,vector<int> &vi) {
    if (root==NULL) return;
    vi.push_back(root->data);
    preOrder(root->left,vi);
    preOrder(root->right,vi);
}
void preOrderM (node * root,vector<int> &vi) {
    if (root==NULL) return;
    vi.push_back(root->data);
    preOrderM(root->right,vi);
    preOrderM(root->left,vi);
}
int num=0;
int N;
void postOrder (node * root) {
    if (root==NULL) return;
    postOrder (root->left);
    postOrder (root->right);
    printf ("%d",root->data);
    num++;
    if (num<N) printf (" ");
}
void postOrderM (node * root) {
    if (root==NULL) return;
    postOrderM (root->right);
    postOrderM (root->left);
    printf ("%d",root->data);
    num++;
    if (num<N) printf (" ");
}
int main () {
    scanf ("%d",&N);
    node * root=NULL;
    int child;
    vector<int> ans;
    for (int i=0;i<N;i++) {
        scanf ("%d",&child);
        create (root,child);
        ans.push_back(child);
    }
    vector<int> pre,preM,post;
    preOrder(root,pre);
    preOrderM(root,preM);
    if (pre==ans) {
        printf ("YES\n");
        postOrder (root);
    }
    else if (preM==ans) {
        printf ("YES\n");
        postOrderM (root);
    }
    else printf ("NO");
    return 0;
}
View Code

L2-005 集合相似度

#include<cstdio> 
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100;
set<int>s[maxn],temp;
int main(){
    int n,k;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int m,x;
        scanf("%d",&m);
        for(int j=0;j<m;j++){
            scanf("%d",&x);
            s[i].insert(x) ;
        }
    }
    scanf("%d",&k);
    for(int i=0;i<k;i++){
        int a1,a2,up,down;
        scanf("%d%d",&a1,&a2);
        int same=0,dif=s[a2].size() ;
        set<int>::iterator it;
        for(it=s[a1].begin() ;it!=s[a1].end() ;it++){
            if(s[a2].find(*it)!=s[a2].end() ) same++;
            else dif++;
        }
        printf("%.2f%\n",(double)100.0*same/dif) ;
    }
    return 0;
}
View Code

L2-006 树的遍历

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=1010;
struct node {
    int data;
    node * left;
    node * right;
};
int pre[maxn],in[maxn],post[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;
}
int num=0;
int N;
void BFS (node * root) {
    queue<node *> q;
    q.push(root);
    while (!q.empty()) {
        node * now=q.front();
        q.pop();
        printf ("%d",now->data);
        num++;
        if (num<N) printf (" ");
        if (now->left) q.push(now->left);
        if (now->right) q.push(now->right);
    }
}
int main () {
    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

L2-007 家庭房产

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=10010;
struct people {
    int id,fatherid,motherid,num,area;
    int child[15];
}p[maxn];
struct family {
    int id,renshu;
    double num,area;
    bool flag=false;
}adj[maxn]; 
int father[maxn];
bool visit[maxn]={false};
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 () {
    for (int i=0;i<maxn;i++)
    father[i]=i;
    int N,k,cnt=0;
    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].child[j],p[i].id);
        }
        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].flag=true;
        adj[id].num+=p[i].num;
        adj[id].area+=p[i].area;
    }
    for (int i=0;i<maxn;i++) {
        if (visit[i]) {
            adj[findfather(i)].renshu++;
        }
        if (adj[i].flag)
        cnt++;
    }
    for (int i=0;i<maxn;i++)
    if (adj[i].flag) {
        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\n",cnt);
    sort (adj,adj+maxn,cmp1);
    for (int i=0;i<cnt;i++) {
        printf ("%04d %d %.3f %.3f\n",adj[i].id,adj[i].renshu,adj[i].num,adj[i].area);
    }
    return 0;
}
View Code

L2-008 最长对称子串

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int main () {
    char s[1005];
    int w=0;
    while ((s[w]=getchar())!='\n')
    w++;
    s[w]='\0';
    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

L2-009 抢红包

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=10010;
struct node {
    double money=0;
    int num;
    int qiang=0;
}Node[maxn];
bool cmp (node a,node b) {
    if (a.money!=b.money) return a.money>b.money;
    else if (a.qiang!=b.qiang) return a.qiang>b.qiang;
    else return a.num<b.num;
}
int main () {
    int N;
    scanf ("%d",&N);
    int k,ni,pi;
    for (int i=1;i<=N;i++) {
        scanf ("%d",&k);
        Node[i].num=i;
        for (int j=0;j<k;j++) {
            scanf ("%d %d",&ni,&pi);
            Node[i].money-=pi;
            Node[ni].money+=pi;
            Node[ni].num=ni;
            Node[ni].qiang++;
        }
    }
    sort (Node+1,Node+N+1,cmp);
    for (int i=1;i<=N;i++) {
        printf ("%d %.2f\n",Node[i].num,Node[i].money/100);
    }
    return 0;
}
View Code

L2-010 排座位

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1010;
int N,M,K;
int father[maxn];
int g[maxn][maxn];
void init () {
    for (int i=1;i<=N;i++)
    father[i]=i;
    for (int i=1;i<=N;i++)
    for (int j=1;j<=N;j++)
    g[i][j]=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 %d %d",&N,&M,&K);
    int u,v,t;
    init ();
    for (int i=0;i<M;i++) {
        scanf ("%d %d %d",&u,&v,&t);
        if (t==1) Union (u,v);
        else g[u][v]=g[v][u]=1;
    }
    for (int i=0;i<K;i++) {
        scanf ("%d %d",&u,&v);
        if (findfather(u)==findfather(v)&&g[u][v]==0) {
            printf ("No problem\n");
        }
        else if (findfather(u)!=findfather(v)&&g[u][v]==0) {
            printf ("OK\n");
        }
        else if (findfather(u)==findfather(v)&&g[u][v]==1) {
            printf ("OK but...\n");
        }
        else if (findfather(u)!=findfather(v)&&g[u][v]==1) {
            printf ("No way\n");
        }
    }
    return 0;
}
View Code

L2-011 玩转二叉树

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=1010;
int pre[maxn],in[maxn];
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->right=create (preL+1,preL+numleft,inL,k-1);
    root->left=create (preL+numleft+1,preR,k+1,inR);
    return root;
}
int num=0,N;
void BFS (node * root) {
    queue<node *> q;
    q.push(root);
    while (!q.empty()) {
        node * now=q.front();
        q.pop();
        printf ("%d",now->data);
        num++;
        if (num<N) printf (" ");
        if (now->left) q.push(now->left);
        if (now->right) q.push(now->right);
    }
}
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",&pre[i]);
    node * root=create (0,N-1,0,N-1);
    BFS (root);
    return 0;
}
View Code

L2-012 关于堆的判断

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
int a[maxn],cnt=0;
void upAdjust (int low,int high) {
    int i=high,j=i/2;
    while (j>=low) {
        if (a[j]>a[i]) {
            swap(a[j],a[i]);
            i=j;
            j=i/2;
        }
        else break;
    }
}
void insert (int x) {
    a[++cnt]=x;
    upAdjust (1,cnt);
}
unordered_map<int,int> pos;
int main () {
    int N,M;
    scanf ("%d %d",&N,&M);
    int x;
    for (int i=1;i<=N;i++) {
        scanf ("%d",&x);
        insert (x);
    }
    for (int i=1;i<=N;i++)
    pos[a[i]]=i;
    string s;
    getchar ();
    for (int i=0;i<M;i++) {
        getline (cin,s);
        if (s.substr(s.length()-4,4)=="root") {
            int ans=0,f=1;
            for (int j=0;j<s.length();j++) {
                if (s[j]=='-') f=-1;
                else if (s[j]>='0'&&s[j]<='9') ans=ans*10+s[j]-'0';
            }
            ans*=f;
            if (a[1]==ans) printf ("T\n");
            else printf ("F\n");
        }
        else if (s.substr(s.length()-8,8)=="siblings") {
            int ans[2]={0},top=0,f[2]={1,1};
            for (int j=0;j<s.length();j++) {
                if (s[j]=='-') f[top]=-1;
                else if (s[j]>='0'&&s[j]<='9') ans[top]=ans[top]*10+s[j]-'0';
                else if (s[j-1]>='0'&&s[j-1]<='9') top++;
            }
            ans[0]*=f[0];
            ans[1]*=f[1];
            if (pos[ans[0]]/2==pos[ans[1]]/2) printf ("T\n");
            else printf ("F\n");
        }
        else if (s.find("parent")!=string::npos) {
            int ans[2]={0},top=0,f[2]={1,1};
            for (int j=0;j<s.length();j++) {
                if (s[j]=='-') f[top]=-1;
                else if (s[j]>='0'&&s[j]<='9') ans[top]=ans[top]*10+s[j]-'0';
                else if (s[j-1]>='0'&&s[j-1]<='9') top++;
            }
            ans[0]*=f[0];
            ans[1]*=f[1];
            if (pos[ans[1]]==pos[ans[0]]*2||pos[ans[1]]==pos[ans[0]]*2+1) printf ("T\n");
            else printf ("F\n");
        }
        else if (s.find("child")!=string::npos) {
            int ans[2]={0},top=0,f[2]={1,1};
            for (int j=0;j<s.length();j++) {
                if (s[j]=='-') f[top]=-1;
                else if (s[j]>='0'&&s[j]<='9') ans[top]=ans[top]*10+s[j]-'0';
                else if (s[j-1]>='0'&&s[j-1]<='9') top++;
            }
            ans[0]*=f[0];
            ans[1]*=f[1];
            if (pos[ans[0]]==pos[ans[1]]*2||pos[ans[0]]==pos[ans[1]]*2+1) printf ("T\n");
            else printf ("F\n");
        }
    }
    return 0;
}
View Code

L2-014 列车调度

#include <iostream>
#include <set>
using namespace std;
int main() {
    int n, t;
    cin >> n;
    set<int> s;
    s.insert(0);
    for(int i = 0; i < n; i++) {
        cin >> t;
        if(t < *s.rbegin())
            s.erase(*(s.upper_bound(t)));
        s.insert(t);
    }
    cout << s.size() - 1;
    return 0;
}
View Code

L2-016 愿天下有情人都是失散多年的兄妹

#include <cstdio>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
struct node {
    int f, m, sex;
};
node v[100010];
int level[100010];
bool exist[100010];
int main() {
    int n, m, id, father, mother, a, b;
    scanf("%d", &n);
    char c;
    for(int i = 0; i < n; i++) {
        scanf("%d %c %d %d", &id, &c, &father, &mother);
        v[id].f = father, v[id].m = mother;
        if(c == 'M') v[id].sex = 0;
        else v[id].sex = 1;
        exist[id] = true;
        v[father].sex = 0;
        v[mother].sex = 1;
    }
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        fill(level, level + 100010, 0);
        scanf("%d%d", &a, &b);
        if(v[a].sex == v[b].sex) {
            printf("Never Mind\n");
            continue;
        }
        queue<int> q;
        q.push(a);
        q.push(b);
        level[a] = 1;
        level[b] = 1;
        set<int> s;
        int flag = 0;
        while(!q.empty()) {
            int top = q.front();
            q.pop();
            int size = s.size();
            s.insert(top);
            if(size == s.size()) {
                printf("No\n");
                flag = 1;
                break;
            }
            if(exist[top] == false) continue;
            if(level[top] <= 4) {
                int fa = v[top].f;
                int mo = v[top].m;
                if(fa != -1) {
                    q.push(fa);
                    level[fa] = level[top] + 1;
                }
                if(mo != -1) {
                    q.push(mo);
                    level[mo] = level[top] + 1;
                }
            }
        }
        if(flag == 0) printf("Yes\n");
    }
    return 0;
}
View Code

L2-019 悄悄关注

#include<cstdio>
#include<map>
#include<string>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=100010;
map<string,int> mp1;
map<int,string> mp2;
int hash1[maxn];
int num[maxn];
int main () {
    int N;
    scanf ("%d",&N);
    getchar ();
    string s;
    int cnt=1;
    for (int i=0;i<N;i++) {
        cin>>s;
        mp1[s]=cnt;
        hash1[cnt]=1;
        mp2[cnt++]=s;
    }
    int M,x,sum=0;
    scanf ("%d",&M);
    for (int i=0;i<M;i++) {
        getchar ();
        cin>>s>>x;
        if (mp1[s]==0) {
            mp1[s]=cnt;
            mp2[cnt++]=s;
            num[mp1[s]]=x;
        }
        else num[mp1[s]]=x;
        sum+=x;
    }
    double avg=(sum*1.0)/M;
    vector<string> vi;
    for (int i=1;i<cnt;i++) {
        if (hash1[i]==0&&num[i]>avg) 
        vi.push_back(mp2[i]);
    }
    sort (vi.begin(),vi.end());
    if (vi.size()==0) {
        printf ("Bing Mei You");
        return 0;
    }
    for (int i=0;i<vi.size();i++)
    cout<<vi[i]<<endl;
    return 0;
} 
View Code

L2-020 功夫传人

#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=1000010;
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 ("%d",(int)sum);
}
View Code

L2-021 点赞狂魔

#include<cstdio>
#include<algorithm>
#include<map>
#include<iostream>
#include<string>
#include<set>
using namespace std;
const int maxn=10010;
map<string,int> mp1;
map<int,string> mp2;
struct node {
    string id;
    int num;
    double avg;
}Node[maxn];
bool cmp1 (node a,node b) {
    if (a.num!=b.num) return a.num>b.num;
    else return a.avg<b.avg;
}
int main () {
    set<int> s[maxn];
    int N,cnt=1,K,x;
    string s1;
    scanf ("%d",&N);
    getchar ();
    for (int i=0;i<N;i++) {
        cin>>s1;
        mp1[s1]=cnt;
        mp2[cnt]=s1;
        scanf ("%d",&K);
        for (int j=0;j<K;j++) {
            scanf ("%d",&x);
            s[cnt].insert(x);
        }
        Node[cnt].id=s1;
        Node[cnt].num=s[cnt].size();
        Node[cnt].avg=K*1.0/Node[cnt].num;
        cnt++;
    }
    sort (Node,Node+cnt,cmp1);
    if (cnt>=4) {
        for (int i=0;i<3;i++) {
            if (i!=0) printf (" ");
            cout<<Node[i].id;
        }
    }
    else if (cnt==3) {
        for (int i=0;i<2;i++) {
            if (i!=0) printf (" ");
            cout<<Node[i].id;
        }
        printf (" -");
    }
    else if (cnt==2) {
        for (int i=0;i<1;i++) {
            if (i!=0) printf (" ");
            cout<<Node[i].id;
        }
        printf (" - -");
    }
    else if (cnt==1) {
        printf ("- - -");
    }
    return 0;
}
View Code

L2-022 重排链表

#include <iostream>
#include <vector>
using namespace std;
struct node{
    int id, data, next;
};
int main() {
    int begin, n;
    cin >> begin >> n;
    node a[100010];
    vector<node> v, ans;
    for(int i = 0; i < n; i++) {
        int tbegin, tdata, tnext;
        cin >> tbegin >> tdata >> tnext;
        a[tbegin] = {tbegin, tdata, tnext};
    }
    while(begin != -1) {
        v.push_back(a[begin]);
        begin = a[begin].next;
    }
    int l = 0, r = v.size() - 1;
    while(1) {
        ans.push_back(v[r]);
        r--;
        if((r + 1) - (l - 1) == 1) break;
        ans.push_back(v[l]);
        l++;
        if((r + 1) - (l - 1) == 1) break;
    }
    for(int i = 0; i < ans.size(); i++) {
        if(i != ans.size() - 1)
            printf("%05d %d %05d\n", ans[i].id, ans[i].data, ans[i+1].id);
        else
            printf("%05d %d -1\n", ans[i].id, ans[i].data);
    }
    return 0;
}
View Code

L2-023 图着色问题

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=10010;
vector<int> g[maxn];
int color[maxn];
int V,E,K,N;
int judge () {
    for (int i=1;i<=V;i++) 
    for (int j=0;j<g[i].size();j++)
    if (color[i]==color[g[i][j]]) return 0;
    return 1;
}
int main () {
    scanf ("%d %d %d",&V,&E,&K);
    int u,v;
    for (int i=0;i<E;i++) {
        scanf ("%d %d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    scanf ("%d",&N);
    vector<int> vi;
    int num,ans;
    for (int i=0;i<N;i++) {
        ans=0;
        int course[100010]={0};
        for (int j=1;j<=V;j++) {
            scanf ("%d",&color[j]);
            if (course[color[j]]==0) {
                ans++;course[color[j]]=1;
            }
        }
        if (ans!=K) {
            vi.push_back(0);continue;
        }
        else if (judge()) vi.push_back(1);
        else vi.push_back(0);
    }
    for (int i=0;i<N;i++) {
        if (vi[i]==0) printf ("No\n");
        else printf ("Yes\n");
    }
    return 0;
}
View Code

L2-024 部落

#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\n",maxnum,ans);
    for (int i=0;i<vi.size();i++) {
        if (vi[i]==0) printf ("N\n");
        else printf ("Y\n");
    }
    return 0;
}
View Code

L2-025 分而治之

#include<cstdio> 
#include<vector>
#include<algorithm> 
using namespace std;
const int maxn=1e4+10;
struct Node{
    int v,u;
}node[maxn];
bool vis[maxn];
int main(){
    int n,m,k;
    scanf("%d%d",&n,&m) ;
    for(int i=1;i<=m;i++){
        scanf("%d%d",&node[i].v ,&node[i].u );
    }
    int np,x;
    scanf("%d",&k);
    for(int i=0;i<k;i++){
        fill(vis,vis+maxn,false);
        bool flag=true;
        scanf("%d",&np);
        for(int j=0;j<np;j++){
            scanf("%d",&x);
            vis[x]=true;
        }
        for(int i=1;i<=m;i++){
            if(vis[node[i].u]==true||vis[node[i].v]==true){
                continue;
            }else{
                flag=false;
                break;
            }
        }
        if(flag==true) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
View Code

L2-026 小子辈

#include<cstdio>
#include<vector>
using namespace std;
const int maxn=100010;
vector<int> g[maxn];
int maxl=-1;
vector<int> l[maxn];
void dfs (int root,int depth) {
    l[depth].push_back(root);
    if (depth>maxl) maxl=depth;
    for (int i=0;i<g[root].size();i++)
    dfs (g[root][i],depth+1);
}
int main () {
    int N,root,x;
    scanf ("%d",&N);
    for (int i=1;i<=N;i++) {
        scanf ("%d",&x);
        if (x==-1) root=i;
        else g[x].push_back(i);
    }
    dfs (root,1);
    printf ("%d\n",maxl);
    for (int i=0;i<l[maxl].size();i++) {
        if (i!=0) printf (" ");
        printf ("%d",l[maxl][i]);
    }
    return 0;
}
View Code

L2-027 名人堂与代金券

#include<cstdio>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=10010;
struct node {
    string id;
    int score;
}Node[maxn];
bool cmp1 (node a,node b) {
    if (a.score!=b.score) return a.score>b.score;
    else return a.id<b.id;
}
int main () {
    int N,G,K;
    scanf ("%d %d %d",&N,&G,&K);
    string s;
    int score;
    int ans=0;
    for (int i=0;i<N;i++) {
        getchar ();
        cin>>s>>score;
        Node[i].id=s;
        Node[i].score=score;
        if (score>=G) ans+=50;
        else if (score>=60) ans+=20;
    }
    printf ("%d\n",ans);
    sort (Node,Node+N,cmp1);
    int r=1;
    cout<<r<<" "<<Node[0].id<<" "<<Node[0].score<<endl;
    for (int i=1;i<N;i++) {
        if (Node[i].score==Node[i-1].score) {
            cout<<r<<" "<<Node[i].id<<" "<<Node[i].score<<endl;
        }
        else {
            r=i+1;
            if (r>K) break; 
            cout<<r<<" "<<Node[i].id<<" "<<Node[i].score<<endl;
        }
    }
    return 0;
}
View Code

L2-028 秀恩爱分得快

#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
const int maxn=1010;
vector<int> adj[maxn];
double ga[maxn]={0};
double gb[maxn]={0};
vector<int> pa,pb;
int sex[maxn]={0};
int read (string s) {
    int len=s.length();
    int num;
    if (s[0]!='-') {
        for (int i=0;i<len;i++) {
            num=num*10+s[i]-'0';
        }
        sex[num]=1;
    }
    else {
        for (int i=1;i<len;i++) {
            num=num*10+s[i]-'0';
        }
        sex[num]=-1;
    }
    return num;
} 
int main () {
    int N,M,K,flag;
    scanf ("%d %d",&N,&M);
    for (int i=0;i<N;i++)
    ga[i]=0;
    for (int i=0;i<N;i++)
    gb[i]=0;
    char s[5];
    for (int i=0;i<M;i++) {
        scanf ("%d",&K);
        for (int j=0;j<K;j++) {
            cin>>s;
            int t=read(s);
            adj[i].push_back(t);
        }
    }
    string A,B;
    cin>>A>>B;
    int a=abs(stoi(A));
    int b=abs(stoi(B));
    double maxa=0,maxb=0;
    for (int i=0;i<M;i++) {
        flag=find(adj[i].begin(),adj[i].end(),a)!=adj[i].end();
        if (flag!=0) {
            int k=adj[i].size();
            for (int j=0;j<k;j++) 
            if (sex[adj[i][j]]*sex[a]<0){
            ga[adj[i][j]]+=1.0/k;
            maxa=max(maxa,ga[adj[i][j]]);
            }
        }
    }
    
    for (int i=0;i<M;i++) {
        flag=find(adj[i].begin(),adj[i].end(),b)!=adj[i].end();
        if (flag!=0) {
            int k=adj[i].size();
            for (int j=0;j<k;j++) 
            if (sex[adj[i][j]]*sex[b]<0) {
            gb[adj[i][j]]+=1.0/k;
            maxb=max(maxb,gb[adj[i][j]]);
            }
        }
    }
    for (int i=0;i<N;i++)
    if (ga[i]==maxa&&sex[a]*sex[i]<0) pa.push_back(i);
    for (int i=0;i<N;i++)
    if (gb[i]==maxb&&sex[b]*sex[i]<0) pb.push_back(i);
    //printf ("%.2f %.2f\n",ga[0],gb[3]);
    if (ga[b]==maxa&&gb[a]==maxb) 
    cout<<A<<" "<<B;
    else {
        for (int i=0;i<pa.size();i++) {
            cout<<A;
            if (sex[pa[i]]>0) printf (" %d\n",pa[i]);
            else printf (" -%d\n",pa[i]);
        }
        for (int i=0;i<pb.size();i++) {
            cout<<B;
            if (sex[pb[i]]>0) printf (" %d\n",pb[i]);
            else printf (" -%d\n",pb[i]);
        }
    }
    return 0;
}
View Code

L2-029 特立独行的幸福

#include<bits/stdc++.h>
using namespace std;
const int maxn=10014;
bool chuxian[maxn]={false};
int dulixing[maxn]={0};
int A,B;
int diedai (int n) {
    string s=to_string(n);
    int ans=0;
    for (int i=0;i<s.length();i++)
    ans+=(s[i]-'0')*(s[i]-'0');
    return ans;
} 
int isprime (int n) {
    if (n<=1) return 0;
    if (n==2) return 1;
    if (n%2==0) return 0;
    for (int i=3;i*i<=n;i++)
    if (n%i==0) return 0;
    return 1;
}
int judge (int n) {
    if (n==1) {
        dulixing[n]+=1;
        return 1;
    }
    int a=n;
    int cnt=0;
    bool ischuxian[maxn]={false};
    while (a!=1) {
        cnt++;
        a=diedai(a);
        if (ischuxian[a]==true) {
            return 0;
        }
        ischuxian[a]=true;
        if (a>=A&&a<=B) 
        chuxian[a]=true;
    }
    if (chuxian[n]==true) return 0;
    if (isprime(n)) dulixing[n]+=2*cnt;
    else dulixing[n]+=cnt;
    return 1;
}
int main () {
    scanf ("%d %d",&A,&B);
    vector<pair<int,int>> vi;
    for (int i=A;i<=B;i++)
    judge(i);
    for (int i=B;i>=A;i--) {
        if (judge(i)) {
            vi.push_back({i,dulixing[i]/2});
        }
    }
    for (int i=vi.size()-1;i>=0;i--)
    printf ("%d %d\n",vi[i].first,vi[i].second);
    if (vi.size()==0) printf ("SAD");
    return 0;
}
View Code

L2-030 冰岛人

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
int father[maxn];
void init () {
    for (int i=0;i<maxn;i++)
    father[i]=i;
}
int findfather (int x) {
    while (x!=father[x])
    x=father[x];
    return x;
} 
int judge (int a,int b) {
    int cnt=1;
    while (a!=father[a]&&b!=father[b]) {
        a=father[a];
        b=father[b];
        cnt++;
        if (cnt==5) return 1;
        if (a==b) return 0;
    }
    return 0;
}
bool sex[maxn];
unordered_map<string,int> pos;
int main () {
    string s1,s2,s3,s4;
    string in[maxn][2];
    int N;
    scanf ("%d",&N);
    init ();
    int ans=0;
    for (int i=1;i<=N;i++) {
        cin>>s1>>s2;
        in[i][0]=s1;
        in[i][1]=s2;
        pos[s1]=i;
    }
    //cout<<in[N][1].substr(in[N][1].length()-4,4)<<endl;
    for (int i=1;i<=N;i++) {
        if (in[i][1].length()>4&&in[i][1].substr(in[i][1].length()-4,4)=="sson") {
            sex[pos[in[i][0]]]=true;
            father[pos[in[i][0]]]=pos[in[i][1].substr(0,in[i][1].length()-4)];
        }
        else if (in[i][1].length()>7&&in[i][1].substr(in[i][1].length()-7,7)=="sdottir") {
            sex[pos[in[i][0]]]=false;
            father[pos[in[i][0]]]=pos[in[i][1].substr(0,in[i][1].length()-7)];
        }
        else if (in[i][1].substr(in[i][1].length()-1,1)=="m") sex[pos[in[i][0]]]=true;
        else sex[pos[in[i][0]]]=false;
    }
    int M;
    scanf ("%d",&M);
    for (int i=0;i<M;i++) {
        cin>>s1>>s2>>s3>>s4;
        if (pos[s1]==0||pos[s3]==0) printf ("NA\n");
        else if (sex[pos[s1]]==sex[pos[s3]]) printf ("Whatever\n");
        else if (findfather(pos[s1])!=findfather(pos[s3])) 
        printf ("Yes\n");
        else if (judge(pos[s1],pos[s3])) 
        printf ("Yes\n");
        else printf ("No\n");
    }
    return 0;
}
View Code

L2-031 深入虎穴

#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
const int maxn=100010;
vector<int> g[maxn];
int start[maxn]={0};
int index[maxn]={0};
void BFS (int st) {
    queue<int> q;
    q.push(st);
    index[st]=0;
    while (!q.empty()) {
        int now=q.front();
        q.pop();
        for (int i=0;i<g[now].size();i++) { 
        q.push(g[now][i]);
        index[g[now][i]]=index[now]+1;
    }
    }
}
int main () {
    int N;
    scanf ("%d",&N);
    int v,k;
    for (int i=1;i<=N;i++) {
        scanf ("%d",&k);
        for (int j=0;j<k;j++) {
            scanf ("%d",&v);
            start[v]=1;
            g[i].push_back(v);
        }
    }
    int st;
    for (st=1;st<=N;st++) 
    if (start[st]==0) break;
    BFS (st);
    int maxnum=1,max=0;
    for (int i=1;i<=N;i++) 
    if (index[i]>max) {
        max=index[i];maxnum=i;
    }
    printf ("%d\n",maxnum);
    return 0;
}
View Code

L2-032 彩虹瓶

#include<cstdio>
#include<stack>
#include<vector>
using namespace std;
int main () {
    int N,M,K;
    scanf ("%d %d %d",&N,&M,&K);
    while (K--) {
        stack<int> s;
        int flag=1;
        int cnt=1;
        for (int i=1;i<=N;i++) {
            int temp;
            scanf ("%d",&temp);
            if (temp!=cnt) {
                s.push(temp);
                if (s.size()>M) 
                flag=0;
            }
            else {
                cnt++;
                while (!s.empty()) {
                    if (s.top()==cnt) {
                        s.pop();
                        cnt++;
                    }
                    else break;
                }
            }
        }
        if (flag&&cnt>=N) printf ("YES\n");
        else printf ("NO\n");
    }
    return 0;
}
View Code

L3-001 凑零钱

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int dp[10010],w[10010];
bool choice[10010][110];
bool 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,index=n;
        while (v>0) {
            if (choice[index][v]==true) {
                arr.push_back(w[index]);
                v-=w[index];
            }
            index--;
        }
        for (int i=0;i<arr.size();i++) {
            if (i!=0) printf (" ");
            printf ("%d",arr[i]);
        }
    }
    return 0;
}
View Code

L3-003 社交集群

#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\n",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

L3-004 肿瘤诊断

#include<cstdio>
#include<queue>
using namespace std;
struct node {
    int x,y,z;
};
int m,n,l,t;
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};
int arr[1300][130][80];
bool visit[1300][130][80]={false};
int judge (int x,int y,int z) {
    if (x<0||x>=m||y<0||y>=n||z<0||z>=l)
    return 0;
    if (arr[x][y][z]==0||visit[x][y][z]==true)
    return 0;
    return 1;
}
int bfs (int x,int y,int z) {
    int cnt=0;
    node temp;
    temp.x=x;
    temp.y=y;
    temp.z=z;
    queue<node> q;
    q.push(temp);
    visit[x][y][z]=true;
    while (!q.empty()) {
        node top=q.front();
        q.pop();
        cnt++;
        for (int i=0;i<6;i++) {
            int tx=top.x+X[i];
            int ty=top.y+Y[i];
            int tz=top.z+Z[i];
            if (judge(tx,ty,tz)) {
                visit[tx][ty][tz]=true;
                temp.x=tx;
                temp.y=ty;
                temp.z=tz;
                q.push(temp);
            }
        }
    }
    if (cnt>=t) return cnt;
    else return 0;
}
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",&arr[j][k][i]);
    int ans=0;
    for (int i=0;i<l;i++)
    for (int j=0;j<m;j++)
    for (int k=0;k<n;k++)
    if (arr[j][k][i]==1&&visit[j][k][i]==false)
    ans+=bfs(j,k,i);
    printf ("%d",ans);
    return 0;
}
View Code

L3-005 垃圾箱分布

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXV=1020;
const int INF=1000000000;
int n,m,k,DS,G[MAXV][MAXV];
int d[MAXV];
bool vis[MAXV]={false};
void Dijkstra (int s) {
    memset (vis,false,sizeof(vis));
    fill (d,d+MAXV,INF);
    d[s]=0;
    for (int i=1;i<=n+m;i++) {
        int u=-1,MIN=INF;
        for (int j=1;j<=n+m;j++) {
            if (vis[j]==false&&d[j]<MIN) {
                u=j;MIN=d[j];
            }
        }
        if (u==-1) return;
        vis[u]=true;
        for (int v=1;v<=n+m;v++) {
            if (vis[v]==false&&G[u][v]!=INF) {
                if (d[u]+G[u][v]<d[v])
                d[v]=d[u]+G[u][v];
            }
        }
    }
}
int getID (char str[]) {
    int i=0,len=strlen(str),ID=0;
    while (i<len) {
        if (str[i]!='G') {
            ID=ID*10+(str[i]-'0');
        }
        i++;
    }
    if (str[0]=='G') return n+ID;
    else return ID;
}
int main () {
    scanf ("%d%d%d%d",&n,&m,&k,&DS);
    int u,v,w;
    char city1[5],city2[5];
    fill (G[0],G[0]+MAXV*MAXV,INF);
    for (int i=0;i<k;i++) {
        scanf ("%s %s %d",city1,city2,&w);
        u=getID(city1);
        v=getID(city2);
        G[u][v]=G[v][u]=w;
    }
    double ansDis=-1,ansAvg=INF;
    int ansID=-1;
    for (int i=n+1;i<=n+m;i++) {
        double minDis=INF,avg=0;
        Dijkstra(i);
        for (int j=1;j<=n;j++) {
            if (d[j]>DS) {
                minDis=-1;
                break;
            }
            if (d[j]<minDis) minDis=d[j];
            avg+=1.0*d[j]/n;
        }
        if (minDis==-1) continue;
        if (minDis>ansDis) {
            ansID=i;
            ansDis=minDis;
            ansAvg=avg;
        }
        else if (minDis==ansDis&&avg<ansAvg) {
            ansID=i;
            ansAvg=avg;
        }
    }
    if (ansID==-1) printf ("No Solution\n");
    else {
        printf ("G%d\n",ansID-n);
        printf ("%.1f %.1f\n",ansDis,ansAvg);
    }
    return 0;
}
View Code

L3-006 迎风一刀斩

#include<bits/stdc++.h>
using namespace std;
const int maxn=1014;
struct node {
    double x,y;
}Node[2][maxn];
double distance (node a,node b) {
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int isXiebian (node a,node b) {
    if (a.x!=b.x&&a.y!=b.y) return 1;
    return 0;
}
double angle (node a,node b) {
    return fabs(a.x-b.x)/fabs(a.y-b.y);
}
vector<double> edge[2];
int main () {
    int T;
    scanf ("%d",&T);
    int k1,k2;
    while (T--) {
        edge[0].clear();edge[1].clear();
        scanf ("%d",&k1);
        for (int i=0;i<k1;i++) scanf ("%lf %lf",&Node[0][i].x,&Node[0][i].y);
        scanf ("%d",&k2);
        for (int i=0;i<k2;i++) scanf ("%lf %lf",&Node[1][i].x,&Node[1][i].y);
        double xie1=0,xie2=0,cnt1=0,cnt2=0,angle1,angle2,p1,p2;
        for (int i=0;i<k1;i++) {
            int j=i<k1-1?i+1:0;
            if (isXiebian(Node[0][i],Node[0][j])) xie1=distance(Node[0][i],Node[0][j]),cnt1++,
            angle1=angle(Node[0][i],Node[0][j]);
            p1=edge[0].size();
            edge[0].push_back(distance(Node[0][i],Node[0][j]));
        }
        for (int i=0;i<k2;i++) {
            int j=i<k2-1?i+1:0;
            if (isXiebian(Node[1][i],Node[1][j])) xie2=distance(Node[1][i],Node[1][j]),cnt2++,
            angle2=angle(Node[1][i],Node[1][j]);
            p2=edge[1].size();
            edge[1].push_back(distance(Node[1][i],Node[1][j]));
        }
        if (k1+k2>8||k1+k2<6) printf ("NO\n");
        else if (xie1==xie2&&cnt1==1&&cnt2==1) {
            if ((angle1==angle2||angle1*angle2==1)&&(k1!=4||k2!=4)) printf ("YES\n");
            else if (k1==4&&k2==4) {
                double z1=p1<k1-2?p1+2:p1+2-k1;
                double z2=p2<k2-2?p2+2:p2+2-k2;
                double z3=p1<k1-1?p1+1:0;
                double z4=p1>0?p1-1:k1-1;
                double z5=p2<k2-1?p2+1:0;
                double z6=p2>0?p2-1:k2-1;
                if (edge[0][z1]==edge[0][z2]&&(edge[0][z3]+edge[1][z5]==edge[0][z4]+edge[1][z6]||
                edge[0][z3]+edge[1][z6]==edge[0][z4]+edge[1][z5])) printf ("YES\n");
                else printf ("NO\n"); 
            }
            else printf ("NO\n");
        }
        else if (xie1==0&&xie2==0&&k1==4&&k2==4) {
            for (int i=0;i<k1;i++) {
                for (int k=0;k<k2;k++) 
                if (edge[0][i]==edge[1][k]) cnt1++;
            }
            if (cnt1==8||cnt1==16) printf ("YES\n");
            else printf ("NO\n");
        } 
        else printf ("NO\n");
    } 
    return 0;
} 
View Code

L3-008 喊山

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
const int maxn=10014;
int n,l,m,k;
struct node {
    int id,layer;
};
vector<int> g[maxn];
int maxdepth,maxcnt;
void bfs (node tnode) {
    bool inq[maxn]={false};
    queue<node> q;
    q.push(tnode);
    inq[tnode.id]=true;
    int cnt=0;
    while (!q.empty()) {
        node top=q.front();
        q.pop();
        int topid=top.id;
        if (top.layer>maxdepth) {
            maxdepth=top.layer;
            maxcnt=top.id;
        }
        else if (top.layer==maxdepth&&top.id<maxcnt) 
        maxcnt=top.id;
        for (int i=0;i<g[topid].size();i++) {
            int nextid=g[topid][i];
            if (inq[nextid]==false) {
                node next={nextid,top.layer+1};
                q.push(next);
                inq[next.id]=true;
                //cnt++;
            }
        }
    }
    //return cnt;
}
int main () {
    scanf ("%d %d %d",&n,&m,&k);
    //v.resize(n+1);
    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);
    }
    int tid;
    for (int i=0;i<k;i++) {
        scanf ("%d",&tid);
        node tnode={tid,0};
        maxdepth=0,maxcnt=0;
        bfs (tnode);
        if (maxdepth==0) printf ("0\n");
        else printf ("%d\n",maxcnt);
    }
    return 0;
}
View Code

L3-010 是否完全二叉搜索树

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct node {
    int data;
    node * left=NULL;
    node * right=NULL;
};
void insert (node * &root,int x) {
    if (x>root->data) {
        if (root->left) insert (root->left,x);
        else {
            root->left=new node;
            root->left->data=x;
        }
    }
    else {
        if (root->right) insert (root->right,x);
        else {
            root->right=new node;
            root->right->data=x;
        }
    }
}
int num=0,N;
int flag=0,cnt=0;
void BFS (node * root) {
    queue<node *> q;
    q.push(root);
    while (!q.empty()) {
        node * now=q.front();
        q.pop();
        printf ("%d",now->data);
        num++;
        if (num<N) printf (" ");
        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 () {
    scanf ("%d",&N);
    int r;
    scanf ("%d",&r);
    node * root=new node;
    root->data=r;
    root->left=NULL;
    root->right=NULL;
    int x;
    for (int i=1;i<N;i++) {
        scanf ("%d",&x);
        insert (root,x);
    }
    BFS (root);
    printf ("\n");
    if (flag==1) printf ("NO");
    else printf ("YES");
    return 0;
}
View Code

L3-011 直捣黄龙

#include<cstdio>
#include<string>
#include<map>
#include<iostream>
#include<vector>
using namespace std;
const int maxn=1010;
const int inf=1e9;
map<string,int> mp1;
map<int,string> mp2;
int g[maxn][maxn],d[maxn],w[maxn],numNode[maxn],weight[maxn],num[maxn];
bool visit[maxn];
int pre[maxn];
int N,K;
void dijkstra (int s) {
    for (int i=0;i<N;i++) {
        d[i]=inf;
        w[i]=0;
        numNode[i]=0;
        num[i]=0;
    }
    d[s]=0;
    w[s]=0;
    numNode[s]=0;
    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 (g[u][v]!=inf&&visit[v]==false) {
                if (d[u]+g[u][v]<d[v]) {
                    d[v]=d[u]+g[u][v];
                    w[v]=w[u]+weight[v];
                    numNode[v]=numNode[u]+1;
                    num[v]=num[u];
                    pre[v]=u;
                }
                else if (d[u]+g[u][v]==d[v]) {
                    num[v]+=num[u];
                    if (numNode[u]+1>numNode[v]) {
                        numNode[v]=numNode[u]+1;
                        pre[v]=u;
                        w[v]=w[u]+weight[v];
                    }
                    else if (numNode[u]+1==numNode[v]&&w[u]+weight[v]>w[v]) {
                        w[v]=w[u]+weight[v];
                        pre[v]=u;
                    }
                }
            }
        }
    }
}
vector<int> vi;
void dfs (int v) {
    if (v==0) {
        vi.push_back(0);
        return;
    }
    dfs (pre[v]);
    vi.push_back(v);
}
int main () {
    string st,ed;
    cin>>N>>K>>st>>ed;
    mp1[st]=0;
    mp2[0]=st;
    string s;
    for (int i=1;i<N;i++) {
        cin>>s>>weight[i];
        mp1[s]=i;
        mp2[i]=s;
    }
    string u,v;
    int q;
    for (int i=0;i<N;i++)
    for (int j=0;j<N;j++)
    g[i][j]=inf;
    for (int i=0;i<K;i++) {
        cin>>u>>v>>q;
        g[mp1[u]][mp1[v]]=q;
        g[mp1[v]][mp1[u]]=q;
    }
    cout<<st;
    dijkstra (0);
    dfs (mp1[ed]);
    for (int i=1;i<vi.size();i++)
    cout<<"->"<<mp2[vi[i]];
    printf ("\n");
    printf ("%d %d %d",num[mp1[ed]],d[mp1[ed]],w[mp1[ed]]);
    return 0;
} 
View Code

L3-014 周游世界

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
const int maxn=10014;
map<int,int> line;
vector<int> g[maxn];
vector<int> path,tmp;
int transferCnt (vector<int> vi) {
    int cnt=0;
    int pre=vi[0];
    for (int i=1;i<vi.size()-1;i++) {
        if (line[pre*10000+vi[i]]!=line[vi[i]*10000+vi[i+1]])
        cnt++;
        pre=vi[i];
    }
    return cnt;
}
int minTransferCnt=1e9;
int minCnt=1e9;
int st,ed;
bool visit[maxn];
void dfs (int st) {
    tmp.push_back(st);
    visit[st]=true;
    if (st==ed) {
        if (tmp.size()<minCnt) {
            path=tmp;
            minCnt=tmp.size();
            minTransferCnt=transferCnt(tmp);
        }
        else if (tmp.size()==minCnt&&transferCnt(tmp)<minTransferCnt) {
            path=tmp;
            minTransferCnt=transferCnt(tmp);
        }
        tmp.pop_back();
        return;
    }
    for (int i=0;i<g[st].size();i++) {
    if (visit[g[st][i]]==false) {
        dfs (g[st][i]);
        visit[g[st][i]]=false;
        //tmp.pop_back();
    }
}
    tmp.pop_back();
}
int main () {
    int n,k,pre,x;
    scanf ("%d",&n);
    for (int i=1;i<=n;i++) {
        scanf ("%d %d",&k,&pre);
        for (int j=1;j<k;j++) {
            scanf ("%d",&x);
            g[x].push_back(pre);
            g[pre].push_back(x);
            line[pre*10000+x]=i;
            line[x*10000+pre]=i;
            pre=x;
        }
    }
    int q;
    scanf ("%d",&q);
    for (int i=0;i<q;i++) {
        scanf ("%d %d",&st,&ed);
        fill (visit,visit+maxn,false);
        minCnt=1e9;
        minTransferCnt=1e9;
        tmp.clear ();
        dfs (st);
        visit[st]=false;
        if (minCnt==1e9) {
            printf ("Sorry, no line is available.\n");
            continue;
        }
        printf ("%d\n",minCnt-1);
        int preL=0;
        for (int i=1;i<path.size();i++) {
            if (line[path[i-1]*10000+path[i]]!=line[path[i]*10000+path[i+1]]) {
                printf ("Go by the line of company #%d from %04d to %04d.\n",line[path[i-1]*10000+path[i]],path[preL],path[i]);
                preL=i;
            }
        }
    }
    return 0;
}
View Code

L3-016 二叉搜索树的结构

#include<bits/stdc++.h>
using namespace std;
const int maxn=1014;
struct node {
    int data;
    node * left;
    node * right;    
};
void insert (node * &root,int v) {
    if (root==NULL) {
        root=new node;
        root->data=v;
        root->left=NULL;
        root->right=NULL;
        return; 
    }
    if (v<root->data) insert (root->left,v);
    else insert (root->right,v);
}
int isFull=1,maxdepth=-1;
unordered_map<int,int> bro,l,r,father,depth,pos;
void bfs (node * root) {
    queue<node *> q;
    q.push(root);
    depth[root->data]=0;
    //maxdepth=max (depth[root->data],maxdepth);
    while (!q.empty()) {
        node * now=q.front();
        q.pop();
        //if (!now->left||!now->right) isFull=0;
        if ((!now->left&&now->right)||(now->left&&!now->right)) isFull=0;
        if (now->left&&now->right) {
            bro[now->left->data]=now->right->data;
            bro[now->right->data]=now->left->data;
        }
        if (now->left) {
            q.push(now->left);
            depth[now->left->data]=depth[now->data]+1;
            //maxdepth=max (depth[now->left->data],maxdepth);
            l[now->data]=now->left->data;
            father[now->left->data]=now->data;
        }
        if (now->right) {
            q.push(now->right);
            depth[now->right->data]=depth[now->data]+1;
            r[now->data]=now->right->data;
            father[now->right->data]=now->data;
            //maxdepth=max (depth[now->right->data],maxdepth);
        }
    }
}
int main () {
    int N,x;
    scanf ("%d",&N);
    node * root=NULL;
    for (int i=0;i<N;i++) {
        scanf ("%d",&x);
        insert (root,x);
        pos[x]=1;
    }
    bfs (root);
    //if (N<pow(2,maxdepth)-1) isFull=0;
    int q;
    scanf ("%d",&q);
    string s;
    getchar ();
    for (int i=0;i<q;i++) {
        getline (cin,s);
        //cout<<s.substr(s.length()-1-4,4)<<endl;
        if (s.substr(s.length()-4,4)=="root") {
            int rootnum=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>='0'&&s[j]<='9') rootnum=rootnum*10+s[j]-'0';
                else break;
            }
            if (root->data==rootnum) printf ("Yes\n");
            else printf ("No\n");
        }
        else if (s.substr(s.length()-8,8)=="siblings") {
            int num[2]={0},cnt=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>='0'&&s[j]<='9') num[cnt]=num[cnt]*10+s[j]-'0';
                else if (s[j-1]>='0'&&s[j-1]<='9') cnt++;
            }
            if (pos[num[0]]==0||pos[num[1]]==0) printf ("No\n"); 
            else if (bro[num[0]]==num[1]) printf ("Yes\n");
            else printf ("No\n");
        }
        else if (s.substr(s.length()-4,4)=="tree") {
            if (isFull==1) printf ("Yes\n");
            else printf ("No\n");
        }
        else if (s.substr(s.length()-5,5)=="level") {
            int num[2]={0},cnt=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>='0'&&s[j]<='9') num[cnt]=num[cnt]*10+s[j]-'0';
                else if (s[j-1]>='0'&&s[j-1]<='9') cnt++;
            }
            if (pos[num[0]]==0||pos[num[1]]==0) printf ("No\n"); 
            else if (depth[num[0]]==depth[num[1]]) printf ("Yes\n");
            else printf ("No\n");
        }
        else if (s.find("left")!=string::npos) {
            int num[2]={0},cnt=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>='0'&&s[j]<='9') num[cnt]=num[cnt]*10+s[j]-'0';
                else if (s[j-1]>='0'&&s[j-1]<='9') cnt++;
            }
            if (pos[num[0]]==0||pos[num[1]]==0) printf ("No\n"); 
            else if (l[num[1]]==num[0]) printf ("Yes\n");
            else printf ("No\n");
        }
        else if (s.find("right")!=string::npos) {
            int num[2]={0},cnt=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>='0'&&s[j]<='9') num[cnt]=num[cnt]*10+s[j]-'0';
                else if (s[j-1]>='0'&&s[j-1]<='9') cnt++;
            }
            if (pos[num[0]]==0||pos[num[1]]==0) printf ("No\n");
            else if (r[num[1]]==num[0]) printf ("Yes\n");
            else printf ("No\n");
        }
        else if (s.find("parent")!=string::npos) {
            int num[2]={0},cnt=0;
            for (int j=0;j<s.length();j++) {
                if (s[j]>='0'&&s[j]<='9') num[cnt]=num[cnt]*10+s[j]-'0';
                else if (s[j-1]>='0'&&s[j-1]<='9') cnt++;
            }
            /*if (pos[num[0]]==0||pos[num[1]]==0) printf ("No\n");
            else*/ if (father[num[1]]==num[0]) printf ("Yes\n");
            else printf ("No\n");
        }
    }
    return 0;
}
View Code

L3-020 至多删三个字符

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+14;
string s;
long long dp[5][maxn];
int main () {
    getline(cin,s);
    fill (dp[0],dp[0]+maxn,1);
    for (int i=1;i<=3;i++) {
        for (int j=i;j<=s.length();j++) {
            dp[i][j]=dp[i][j-1]+dp[i-1][j-1];
            for (int k=j-1;j-k<=i;k--) 
            if (s[k-1]==s[j-1]) {
                dp[i][j]-=dp[i-(j-k)][k-1];break;
            }
        } 
    }
    printf ("%lld",dp[0][s.length()]+dp[1][s.length()]+dp[2][s.length()]+dp[3][s.length()]);
    return 0;
} 
View Code

L3-021 地铁一日游

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int inf=1e9;
int d[maxn][maxn];
bool visit[maxn]={false};
bool isend[maxn]={false};
int N,M,K;
vector<int> g[maxn];
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]);
}
void init () {
    for (int i=1;i<=N;i++)
    for (int j=1;j<=N;j++)
    d[i][j]=inf;
}
void floyd () {
    for (int k=1;k<=N;k++)
    for (int i=1;i<=N;i++)
    for (int j=1;j<=N;j++)
    if (i!=j) d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
int main () {
    scanf ("%d %d %d",&N,&M,&K);
    int u,v,x;
    init ();
    for (int i=0;i<M;i++) {
        scanf ("%d",&u);
        isend[u]=true;
        while (1) {
            scanf ("%d %d",&x,&v);
            d[u][v]=min(d[u][v],x);
            d[v][u]=d[u][v];
            u=v;
            char ch=getchar ();
            if (ch=='\n') break;
        }
        isend[u]=true;
    }
    floyd ();
    for (int i=1;i<=N;i++) {
        unordered_map<int,int> pos;
        for (int j=1;j<=N;j++)
        if (i!=j) pos[d[i][j]/K]=max(pos[d[i][j]/K],d[i][j]);
        for (int j=1;j<=N;j++) 
        if (i!=j&&d[i][j]!=inf) {
            if (d[i][j]==pos[d[i][j]/K]||isend[j]==true) 
            g[i].push_back(j);
        }
    }
    int q;
    scanf ("%d",&q);
    for (int i=0;i<q;i++) {
        fill (visit,visit+maxn,false);
        scanf ("%d",&u);
        dfs (u);
        int flag=0;
        for (int j=1;j<=N;j++) {
            if (visit[j]==true) {
                if (flag!=0) printf (" ");
                printf ("%d",j);
                flag++;
            }
        }
        printf ("\n");
    }
    return 0;
}
View Code

L3-023 计算图

#include<bits/stdc++.h>
using namespace std;
const int maxn=50014;
struct node {
    vector<int> pre;
    double data;
    int fuhao=0;
}Node[maxn];
vector<int> g[maxn];
int a[maxn];
int inDegree[maxn];
int N;
stack<int> topOrder;
double getnum (node a) {
    if (a.pre.size()==0) return a.data;
    else if (a.pre.size()==1) {
        if (a.fuhao==4) return exp(Node[a.pre[0]].data);
        else if (a.fuhao==5) return log(Node[a.pre[0]].data);
        else if (a.fuhao==6) return sin(Node[a.pre[0]].data);
    } 
    else if (a.pre.size()==2) {
        if (a.fuhao==1) return Node[a.pre[0]].data+Node[a.pre[1]].data;
        else if (a.fuhao==2) return Node[a.pre[0]].data-Node[a.pre[1]].data;
        else if (a.fuhao==3) return Node[a.pre[0]].data*Node[a.pre[1]].data;
    }
}
double topSort () {
    queue<int> q;
    for (int i=0;i<N;i++)
    if (inDegree[i]==0) q.push(i);
    while (!q.empty()) {
        int u=q.front();
        q.pop();
        topOrder.push(u);
        Node[u].data=getnum(Node[u]);
        for (int i=0;i<g[u].size();i++)
        if (--inDegree[g[u][i]]==0) q.push(g[u][i]);
        if (q.empty()) return Node[u].data;
    }
}
double dfs (int u,int x) {
    if (Node[u].fuhao==0) {
        if (u==x) return 1;
        else return 0;
    }
    else if (Node[u].fuhao==1) return dfs(Node[u].pre[0],x)+dfs(Node[u].pre[1],x);
    else if (Node[u].fuhao==2) return dfs(Node[u].pre[0],x)-dfs(Node[u].pre[1],x);
    else if (Node[u].fuhao==3) return dfs(Node[u].pre[0],x)*Node[Node[u].pre[1]].data+dfs(Node[u].pre[1],x)*Node[Node[u].pre[0]].data;
    else if (Node[u].fuhao==4) return exp(Node[Node[u].pre[0]].data)*dfs(Node[u].pre[0],x);
    else if (Node[u].fuhao==5) return 1.0/Node[Node[u].pre[0]].data*dfs(Node[u].pre[0],x);
    else if (Node[u].fuhao==6) return cos(Node[Node[u].pre[0]].data)*dfs(Node[u].pre[0],x);
}
int main () {
    scanf ("%d",&N);
    int fuhao;
    double x,y;
    vector<int> v1;
    for (int i=0;i<N;i++) {
        scanf ("%d",&fuhao);
        if (fuhao==0) {
            scanf ("%lf",&x);
            Node[i].data=x;
            v1.push_back(i);
        }
        else if (fuhao>=1&&fuhao<=3) {
            scanf ("%lf %lf",&x,&y);
            Node[i].pre.push_back((int)x);
            Node[i].pre.push_back((int)y);
            inDegree[i]+=2;
            g[(int)x].push_back(i);
            g[(int)y].push_back(i);
            Node[i].fuhao=fuhao;
        }
        else if (fuhao>=4&&fuhao<=6) {
            scanf ("%lf",&x);
            Node[i].pre.push_back((int)x);
            inDegree[i]+=1;
            g[(int)x].push_back(i);
            Node[i].fuhao=fuhao;
        }
    }
    printf ("%.3f\n",topSort());
    int u=topOrder.top();
    for (int i=0;i<v1.size();i++) {
        if (i!=0) printf (" ");
        printf ("%.3f",dfs(u,v1[i]));
    }
    return 0;
}
View Code

 

posted @ 2020-02-25 14:20  zlc0405  阅读(162)  评论(0编辑  收藏  举报