Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

顶点数20000了么?

Posted by qq475751511 at 2017-02-04 13:43:40 on Problem 3437
v1和v2开18888过不了
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned int uii;
int n,id,pt,d1,d2,cas;
char s[20005];
vector<int> v1[20005],v2[20005];
void dfs1(int u,int de) {
    d1=max(d1,de);
    while (s[++pt]=='d') {
        v1[u].push_back(++id);
        dfs1(id,de+1);
    }
}
void dfs2(int u) {
    for (uii i=0;i<v1[u].size();++i) {
        if (i==0)
            v2[u].push_back(v1[u][i]);
        else
            v2[v1[u][i-1]].push_back(v1[u][i]);
        dfs2(v1[u][i]);

    }
}
void dfs3(int u,int de) {
    d2=max(d2,de);
    for (uii i=0;i<v2[u].size();++i)
        dfs3(v2[u][i],de+1);
}
int main()
{
    while (scanf("%s",s)==1&&s[0]!='#') {
        pt=-1;
        d1=d2=id=0;
        n=strlen(s);
        for (int i=0;i<n;++i) {
            v1[i].clear();
            v2[i].clear();
        }
        dfs1(0,0);
        dfs2(0);
        dfs3(0,0);
        printf("Tree %d: %d => %d\n",++cas,d1,d2);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator