ICPC North America Qualifier Contest 2015

计蒜客

A. All about that base

The base (or radix) of a positional numeral system is the number of symbols that can be used to represent a number in that system. The base 10 system (also known as decimal) uses 10 distinct symbols: 0,1, … \dots ,9. For example, we interpret the number 72345 as:

7$\timesKaTeX parse error: Double superscript at position 5: 10^4^̲ + 2\timesKaTeX parse error: Double superscript at position 5: 10^3^̲ + 3\timesKaTeX parse error: Double superscript at position 5: 10^2^̲ + 4\timesKaTeX parse error: Double superscript at position 5: 10^1^̲ + 5\times$100.

This example illustrates that in base 10 the symbol at place P ≥ \geq 0 (starting from the right) is multiplied by 10P to get its value. More generally, in base B we use B symbols to represent 0, … \dots ,B-1, and the symbol at the P th \text{th} th place is multiplied by BP to get its value.

Other bases commonly used in computation include base 2 (or binary, using symbols 0 and 1), base 8 (or octal, using symbols 0–7), and base 16 (or hexadecimal, using symbols 0–9 and a–f). In bases higher than 10, letters represent the higher values. Thus in hexadecimal a–f represent the decimal values 10–15, and in bases ≥ \geq 36 the letter z represents the decimal value 35.

Your job is to determine the bases in which given arithmetic expressions are valid. We define an expression as valid in base B if two conditions are true. First, all the operands used are interpretable in base B as having values in the decimal range [ 1 , 2 32 − 1 ] [1,2^{32}-1] [1,2321] . Second, the expression is true. Any arbitrary expression might be valid in zero, one, or more bases. In this problem we will only consider bases 1–36, where base 1 is unary.

Note that following the convention listed above, unary would consist of a single symbol: 0. In this problem, unary numbers use the symbol 1 rather than 0 (think “tally marks”). E.g. \text{E.g.} E.g., 111 in unary is equivalent to the decimal number 3 and 1111111 in unary is equivalent to the decimal number 7.

Input

Input for this problem starts with a line containing an integer 0 ≤ \leq N ≤ \leq 20. The following N lines each contain an arithmetic expression with the following form:

X X X o p op op Y Y Y = = = Z \Z Z

where X,Y , and Z are positive, whole numbers consisting of 1 to 100 symbols from the set 0–9 and a–z, and o p op op is one of the four operators + + +, − − , ∗ ∗ , / / /. For each statement there is at least one base 1 ≤ \leq B ≤ \leq 36 such that X , Y , and Z can all be interpreted in base B as having values in the decimal range [ 1 , 2 32 − 1 ] [1,2^{32}-1] [1,2321].

Output

For each expression, list the bases in which the expression is valid (sorted in ascending base order) or the word “invalid” if the expression not valid in any of the bases 1–36. Use symbols 1–9, then a–z, then 0 to represent bases 1–36 (with the last symbol, 0, representing base 36).

输出时每行末尾的多余空格,不影响答案正确性

样例输入
8
6ef + d1 = 7c0
3 / 2 = 1
444 / 2 = 222
10111 * 11 = 1000101
10111 * 11 = 111221
5k - 1z = 46
1111111111 - 1111111 = 111
2048 - 512 = 1536
样例输出
g
invalid
56789abcdefghijklmnopqrstuvwxyz0
2
3456789abcdefghijklmnopqrstuvwxyz0
invalid
1
a

题解:

基本上算是个水体,主要是1进制需要特别判断一下,

1进制:就是这个仅由1组成,不包括0。它的计算方法符合结绳计数法

例如:1111 + 1111 = 11111111

11 * 11 = 1111

#include<map>
#include<cstdio>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
const int Max=1e5+10;
int Find(string x)
{
    int maxs=0,t;
    for(int i=0;i<x.length();i++)
    {
        if(x[i]>='0'&&x[i]<='9')
            t=x[i]-'0';
        else {
            t=x[i]-'a'+10;
        }
        if(t>maxs)
            maxs=t;
    }
    return maxs;
}
long long Sum(string a,int j)
{
    long long sum=1,aa=0;
    for(int k=a.length()-1;k>=0;k--)
    {
        if(a[k]>='0'&&a[k]<='9')
            aa+=(a[k]-'0')*sum;
        else aa+=(a[k]-'a'+10)*sum;
        sum*=j;
    }
    return aa;
}
long long Sum1(string a)
{
    long long sum=0;
    for(int i=0;i<a.length();i++)
    {
        if(a[i]>='0'&&a[i]<='9')
            sum+=a[i]-'0';
        else sum+=a[i]-'a'+10;
    }
    return sum;
}
int Find1(string a)
{
    for(int i=0;i<a.length();i++)
        if(a[i]!='1')
            return 0;
    return 1;
}
int main()
{
    int n,maxs=0;
    string str,a,b,c,op,x;
    long long aa,bb,cc;
    cin >> n;
    getchar();
    for(int i=0;i<n;i++)
    {
        int ans=0;
        getline(cin,str);
        stringstream ss(str);
        ss >> a,ss >> op,ss >> b,ss >> x,ss >> c;
        maxs=Find(a);
        maxs=max(Find(b),maxs);
        maxs=max(Find(c),maxs);
        if(maxs==1&&Find1(a)&&Find1(b)&&Find1(c))
        {
            aa=Sum1(a);
            bb=Sum1(b);
            cc=Sum1(c);
            if(((op[0]=='+')&&(aa+bb==cc))||((op[0]=='-')&&(aa-bb==cc))||((op[0]=='*')&&(aa*bb==cc))||((op[0]=='/')&&(bb*cc==aa)))
            {
                cout << 1;
                ans++;
            }
        }
        for(int j=maxs+1;j<=36;j++)
        {
            aa=Sum(a,j);
            bb=Sum(b,j);
            cc=Sum(c,j);
            if(((op[0]=='+')&&(aa+bb==cc))||((op[0]=='-')&&(aa-bb==cc))||((op[0]=='*')&&(aa*bb==cc))||((op[0]=='/')&&(bb*cc==aa)))
            {
                char ch;
                if(j<=9)
                    ch=j+'0';
                else if(j!=36)
                    ch=j+'a'-10;
                else ch='0';
                cout << ch;
                ans++;
            }
        }
        if(ans==0)
            cout << "invalid";
        cout << endl;
    }
    return 0;
}

B. Bobby’s Bet

Bobby and Betty have a bet. Betty bets Bobby that he cannot roll an S-sided die (having values 1 through S) and obtain a value ≥ \geq R on at least X out of Y rolls. Betty has a variety of dice with different numbers of sides S, and all her dice are fair (for a given die, each side’s outcome is equally likely). In order to observe statistically rare events while still giving Bobby a reason to bet, Betty offers to pay Bobby W times his bet on each encounter. For example, suppose Betty bets Bobby 1 bitcoin that he can’t roll at least a 5 on a 6-sided die at least two out of three times; if Bobby does, she would give him W = 3 times his initial bet ( i.e. \text{i.e.} i.e. she would give him 3 bitcoins). Should Bobby take the bet (is his expected return greater than his original bet)?

Input

Input begins with an integer 1 ≤ \leq N ≤ \leq 10000 ,representing the number of cases that follow. The next N lines each contain five integers, R, S, X, Y , and W. Their limits are 1 ≤ \leq R ≤ \leq S ≤ \leq 20, 1 ≤ \leq X ≤ \leq Y ≤ \leq 10, and 1 ≤ \leq W ≤ \leq 100.

Output

For each case, output “yes” if Bobby’s expected(期望) return is greater than his bet, or “no” otherwise. Bobby is somewhat risk averse and does not bet if his expected return is equal to his bet.

输出时每行末尾的多余空格,不影响答案正确性

样例输入1
2
5 6 2 3 3
5 6 2 3 4
样例输出1
no
yes
样例输入2
3
2 2 9 10 100
1 2 10 10 1
1 2 10 10 2
样例输出2
yes
no
yes

题解:

听说是个概率题,但是我连题都没有看懂=_=。

A与B打赌,B掷一个S面的骰子,如果B掷Y次骰子,其中有 ≥ \geq X次掷出的骰子的点数 ≥ \geq R,那么就说B赢了,并且获得W(货币)。如果B赢的期望严格大于1,就输出yes,否则输出no。

#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
LL c(LL m,LL n){//组合数
    LL res=1;
    for(int i=1;i<=m;i++)
        res*=i;
    for(int i=1;i<=n;i++)
        res/=i;
    for(int i=1;i<=(m-n);i++)
        res/=i;
    return res;
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        double r,s,x,y,w;
        cin>>r>>s>>x>>y>>w;
        double win=(s-r+1)/(double)s,lose=((r-1)/(double)s);
        double winp=0,losep;
        for(int i=x;i<=y;i++)
        {
            double temp=1;
            for(int j=0;j<i;j++) temp*=win;
            for(int j=0;j<(y-i);j++) temp*=lose;
            temp*=c((int)y,(int)i);
            winp+=temp;
        }
        losep=1-winp;
        double ans=w*winp;
        if(ans>1)
            cout<<"yes\n";
        else cout<<"no\n";
    }
    return 0;
}

C. Cantina of Babel

Characters in Star Wars each speak a language, but they typically understand a lot more languages that they don’t or can’t speak. For example, Han Solo might speak in Galactic Basic and Chewbacca might respond in Shyriiwook; since they each understand the language spoken by the other, they can communicate just fine like this.

We’ll say two characters can converse if they can exchange messages in both directions. Even if they didn’t understand each other’s languages, two characters can still converse as long as there is a sequence of characters who could translate for them through a sequence of intermediate languages. For example, Jabba the Hutt and R2D2 might be able to converse with some help. Maybe when Jabba spoke in Huttese, Boba Fett could translate to Basic, which R2D2 understands. When R2D2 replies in Binary, maybe Luke could translate to Basic and then Bib Fortuna could translate back to Huttese for Jabba.

In Star Wars Episode IV, there’s a scene with a lot of different characters in a cantina, all speaking different languages. Some pairs of characters may not be able to converse (even if others in the cantina are willing to serve as translators). This can lead to all kinds of problems, fights, questions over who shot first, \text{etc.}etc. You’re going to help by asking some of the patrons to leave. The cantina is a business, so you’d like to ask as few as possible to leave. You need to determine the size of the smallest set of characters S such that if all the characters in SS leave, all pairs of remaining characters can converse.

For example, in the first sample input below, Chewbacca and Grakchawwaa can converse, but nobody else understands Shyriiwook, so they can’t converse with others in the bar. If they leave, everyone else can converse. In the second sample input, Fran and Ian can converse, as can Polly and Spencer, but no other pairs of characters can converse, so either everyone but Polly and Spencer must leave or everyone but Fran and Ian.

Input

Input starts with a positive integer, 1 ≤ \leq N ≤ \leq 100, the number of characters in the cantina. This is followed by N lines, each line describing a character. Each of these N lines starts with the character’s name (which is distinct), then the language that character speaks, then a list of 0 to 20 additional languages the character understands but doesn’t speak. All characters understand the language they speak. All character and language names are sequences of 1 to 15 letters ( a-z \text{a-z} a-z and A-Z \text{A-Z} A-Z), numbers, and hyphens. Character names and languages are separated by single spaces.

Output

Print a line of output giving the size of the smallest set of characters S that should be asked to leave so that all remaining pairs of characters can converse.

输出时每行末尾的多余空格,不影响答案正确性

样例输入1
7
Jabba-the-Hutt Huttese
Bib-Fortuna Huttese Basic
Boba-Fett Basic Huttese
Chewbacca Shyriiwook Basic
Luke Basic Jawaese Binary
Grakchawwaa Shyriiwook Basic Jawaese
R2D2 Binary Basic
样例输出1
2
样例输入2
6
Fran French Italian
Enid English German
George German Italian
Ian Italian French Spanish
Spencer Spanish Portugese
Polly Portugese Spanish
样例输出2
4

题解:

题意是有n个人,每一行前两个字符串代表这个人的姓名(保证姓名是不同的)和他们说话使用的语言,后面的字符串代表他们能听懂但是不会说的语言,问最少让多少人离开,能使剩下的每一个人都能沟通。

我的想法是:先列出能听懂第一个人的语言,能听懂第二个人的语言。。。。能听懂第n个人的语言。

如果a能听懂b,b能听懂a,那么a和b就可以连一条线。然后利用并查集将互相能沟通的人归为一组,求某一个组人数最大是多少,这些人全部留下,剩下的人数就全部离开。

但是我还没有Ac,@-@。。。

网上也没有题解,求求大佬们出个题解吧!!!!!!!!!


D. Circuit Counting

Suppose you are given a sequence of N integer-valued vectors in the plane ( x i , y i ) (x_i,y_i) (xi,yi), i=1, … \dots ,N . Beginning at the origin, we can generate a path by regarding each vector as a displacement from the previous location. For instance, the vectors ( 1 , 2 ) , ( 2 , 3 ) , ( − 3 , − 5 ) (1,2),(2,3),(−3,−5) (1,2),(2,3),(3,5) form the path ( 0 , 0 ) , ( 1 , 2 ) , ( 3 , 5 ) , ( 0 , 0 ) (0,0),(1,2),(3,5),(0,0) (0,0),(1,2),(3,5),(0,0). We define a path that ends at the origin as a circuit. The example just given is a circuit. We could form a path using any nonempty subset of the N vectors, while the result (circuit or not) doesn’t depend on the ordering of the subset. How many nonempty subsets of the vectors form circuits?

For instance, consider the vectors { ( 1 , 2 ) , ( − 1 , − 2 ) , ( 1 , 1 ) , ( − 2 , − 3 ) , ( − 1 , − 1 ) } \{(1,2),(−1,−2),(1,1),(−2,−3),(−1,−1)\} {(1,2),(1,2),(1,1),(2,3),(1,1)} From these vectors we can construct 4 possible subset circuits using

{ ( 1 , 2 ) , ( − 1 , − 2 ) } { ( 1 , 1 ) , ( − 1 , − 1 ) } { ( 1 , 2 ) , ( 1 , 1 ) , ( − 2 , − 3 ) } { ( 1 , 2 ) , ( − 1 , − 2 ) , ( 1 , 1 ) , ( − 1 , − 1 ) } \left. \begin{array}{l} \{(1,2),(−1,−2)\}\\ \{(1,1),(−1,−1)\}\\ \{(1,2),(1,1),(−2,−3)\}\\ \{(1,2),(−1,−2),(1,1),(−1,−1)\} \end{array}\right. {(1,2),(1,2)}{(1,1),(1,1)}{(1,2),(1,1),(2,3)}{(1,2),(1,2),(1,1),(1,1)}

Input

Input begins with an integer N ≤ \leq 40 on the first line. The next N lines each contain two integer values x and y forming the vector (x,y), where |x|,|y| ≤ \leq 10 and (x,y) ≠ \neq = (0,0). Since the given vectors are a set, all vectors are unique.

Output

Output the number of nonempty subsets of the given vectors that produce circuits. It’s guaranteed that the answer is less than 1010.

输出时每行末尾的多余空格,不影响答案正确性

样例输入
5
1 2
1 1
-1 -2
-2 -3
-1 -1
样例输出
4

题解:

题意是给n个(x,y),起点为(0,0),求有多少组合数,使得最后(x,y)回到(0,0)点。

思路:二进制暴力肯定会超时,所以要分组分开暴力,题解不是我写的,太懒了,过几天再改吧

#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(x,y) memset(x,y,sizeof(x))
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define db double
#define inf 0x3f3f3f3f
const int mod=1e9+7;
vector<pii>v1,v2;
map<pii,int>m1,m2;
int main()
{
    fast;
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        pii p;
        cin>>p.first>>p.second;
        if(i%2==0)
            v1.pb(p);
        else
            v2.pb(p);//如果用其他方法分组当然也完全ok
    }

    for(int i=1; i<(1<<v1.size()); i++) //枚举所有可能的情况
    {
        pii sum= {0,0};
        for(int j=0; j<v1.size(); j++)
        {
            if((1<<j)&i) //选了第j个
            {
                sum.first+=v1[j].first;
                sum.second+=v1[j].second;
            }
        }
        m1[sum]++;//得到这样的和的方法+1
    }

    for(int i=1; i<(1<<v2.size()); i++) //同上
    {
        pii sum= {0,0};
        for(int j=0; j<v2.size(); j++)
        {
            if((1<<j)&i)
            {
                sum.first+=v2[j].first;
                sum.second+=v2[j].second;
            }
        }
        m2[sum]++;
    }

    ll ans=0;
    for(auto u:m1)
    {
        pii p=u.first;
        if(m2.count({-p.first,-p.second}))
        {
            ans+=m1[ {p.first,p.second}]*(ll)m2[ {-p.first,-p.second}]; //这个应该是显然的吧
        }
    }
    ans+=(m1[ {0,0}]+m2[ {0,0}]); //别忘了加上天然形成的(0,0)
    cout<<ans;
    return 0;
}

G. Safe Passage(经典过河问题)

A group of friends snuck away from their school campus, but now they must return from the main campus gate to their dorm while remaining undetected by the many teachers who patrol the campus. Fortunately, they have an invisibility cloak, but it is only large enough to cover two people at a time. They will take turns as individuals or pairs traveling across campus under the cloak (and by necessity, returning the cloak to the gate if others remain). Each student has a maximum pace at which he or she is able to travel, yet if a pair of students are walking under the cloak together, they will have to travel at the pace of the slower of the two. Their goal is to have everyone back at the dorm as quickly as possible.

As an example, assume that there are four people in the group, with person A able to make the trip in 1 minute, person B able to travel in 2 minutes, person C able to travel in 7 minutes, and person D able to travel in 10 minutes. It is possible to get everyone to the dorm in 17 minutes with the following plan:

– A and B go from the gate to the dorm together (taking 2 minutes)}

– A returns with the cloak to the gate (taking 1 minute)

– C and D go from the gate to the dorm together (taking 10 minutes)

– B returns with the cloak to the gate (taking 2 minutes)

– A and B go from the gate to the dorm together (taking 2 minutes)

Input

The input is a single line beginning with an integer, 2 ≤ \leq N ≤ \leq 15. Following that are N positive integers that respectively represent the minimum time in which each person is able to cross the campus if alone; these times are measured in minutes, with each being at most 5000. (It is a very large campus!)

Output

Output the minimum possible time it takes to get the entire group from the gate to the dorm.

输出时每行末尾的多余空格,不影响答案正确性

样例输入1
2 15 5
样例输出1
15
样例输入2
4 1 2 7 10
样例输出2
17
样例输入3
5 12 1 3 8 6
样例输出3
29

题解:

一个经典的过桥问题,可惜我之前没有接触过。做这个题的时候只想到

  1. 方案一:耗时最小的那个人把剩下的所有人都送走

    例:1 10 10 10 10 10

    很明显让1把5个10送走是最省时的

  2. 方案二:耗时最小的两个人把剩下所有人都送走

    例:1 3 6 8 12

    大门 宿舍 耗时

    1 3 6 8 12

    6 8 12 ---- 1 3 ---- 3 // 1 3 过去

    1 6 8 12 ---- 3 ---- 1 // 1 回去送伞

    1 6 ---- 3 8 12 ---- 12 // 8 12 过去

    1 3 6 ---- 8 12 ---- 3 // 3 回去送伞

    6 ---- 1 3 8 12 ---- 3 // 1 3 过去

    1 6 ---- 3 8 12 ---- 1 // 1 回去送伞

     ----		1 3 6 8 12		----		6			//	1 6	过去
    
  3. 我们从上面的案例中看出规律:最快的两个人(假设为a,b)每送走最慢(假设为c,d)的两个人耗时为:a+b$\times 2 + 2+ 2+\max(b,c)$

    ​ 综上所述:n个人的速度快慢从小到大排序为 a[0],a[1],a[2] … a[n-1]

    1. 如果人数 == 1, All_time=a[0]
    2. 如果人数 == 2, All_time= max ⁡ ( a [ 0 ] , a [ 1 ] ) \max(a[0],a[1]) max(a[0],a[1])
    3. 如果人数 == 3, All_time=a[0]+a[1]+a[2]]
    4. 如果人数 ⩾ \geqslant 4,那么就要贪心,贪心方案一和方案二
//过桥问题
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int Max = 1000;
int a[Max];
int main()
{
    int n,i,sum;
    cin>>n;
    for (i = 0; i < n; i++)
        cin>>a[i];
    sort(a,a+n);
    sum = 0;
    for (i=n-1;i>2;i=i-2)
    {
        //最小者将最大2个送走或最小2个将最大2个送走
        if (a[0]+a[1]+a[1]+a[i]<a[0]+a[0]+a[i-1]+a[i])
            sum=sum+a[0]+a[1]+a[1]+a[i];
        else
            sum=sum+a[0]+a[0]+a[i-1]+a[i];
    }
    if (i==2)
        sum=sum+a[0]+a[1]+a[2];
    else if (i==1)
        sum=sum+a[1];
    else//可能只有一个人
        sum=a[0];
    cout << sum << endl;
    return 0;
}

J. Torn To Pieces

You have arrived in The Big City but your journey is not yet complete. You must still navigate the subway and get to your final destination. The information booth in the subway station is unattended and fresh out of maps of the subway system. On the floor you notice fragments of a map. Can you piece together enough of the map to figure out how to get to your final destination?

Each fragment of the map happens to perfectly contain a single subway station while also identifying all of the other stations that it connects to. Each connection between stations is bi-directional such that it can be travelled going either direction. Using all of the available fragments, your task is to determine the sequence of stations you must pass through in order to reach your final destination or state that there is no route if you don’t have enough information to complete your journey.

J.png

Input

The first line of input has an integer, 2 ≤ \leq N ≤ \leq 32, that identifies the number of pieces of the map that were found.

The following N lines each describe a station depicted on one of those pieces. Each of these lines starts with the name of the station they describe and is followed by a space-separated list of all of the station names that are directly connected to that station (there may be as many as N - 1).

The final line identifies a starting station and a destination station. The destination station is guaranteed to be different than the starting station.

Each station name is a string of up to 20 characters using only letters a–z and A–Z. It is guaranteed that there is at most one simple route (without revisiting stations) from the starting station to the destination station.

Output

Give the sequence of stations that leads from the starting station to the destination station. Separate station names with spaces. If there are not enough pieces of the map to find a route from the starting station to the destination station then output “no route found”.

输出时每行末尾的多余空格,不影响答案正确性

样例输入1
3
Uptown Midtown
Midtown Uptown Downtown
Downtown Midtown
Uptown Downtown
样例输出1
Uptown Midtown Downtown
样例输入2
6
A B
B A D
C D
E D F G
F E
G E
F A
样例输出2
F E D B A
样例输入3
4
FirstStop SecondStop
SecondStop FirstStop ThirdStop
FifthStop FourthStop SixthStop
SixthStop FifthStop
FirstStop FifthStop
样例输出3
no route found

题解:

题意是给出一个站点和其他哪些站点相连,然后给出起点和终点,判断是否可以从起点到达终点,若可以的话就输出路径。输出路径的方法要好好记记。

我的做法是:将字符串转化为数字,然后链式前向星+dfs,用数组存储也可以,数据量不是很大

然后我做的时候有一个坑点是,可能在给你哪些站点相连的时候,没有给出起点和终点,所以在读取起点和终点的时候要判断一下

#include<map>
#include<cstdio>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
const int Max=1e4+10;
map<string,int>mp;//用map将字符串转化为数字
map<int,string>mp1;//在反存一下,便于输出
int cnt,cnt1,s,t;
struct Edge
{
    int v,nxt;
} e[Max];//链式前向星存边
int head[Max],stop[Max],vis[Max],pre[Max];
void add(int u,int v)//建立双向边
{
    e[++cnt].v=v;
    e[cnt].nxt=head[u];
    head[u]=cnt;

    e[++cnt].v=u;
    e[cnt].nxt=head[v];
    head[v]=cnt;
}
int dfs(int s)
{
    if(s==t)
        return 1;
    vis[s]=1;
    for(int i=head[s]; i; i=e[i].nxt)
    {
        int v=e[i].v;
        if(vis[v])
            continue;
        pre[v]=s;//路径存储方式
        if(dfs(v)==1)
            return 1;//一旦找到就马上返回
    }
    vis[s]=0;
    return 0;
}
int main()
{
    int n;
    string str,x,y;
    cin >> n;
    getchar();
    for(int i=0; i<n; i++)
    {
        getline(cin,str);
        stringstream ss(str);
        int num=0;
        while(ss >> x)
        {
            if(mp[x]==0)
                mp[x]=++cnt1;
            stop[num++]=mp[x];
            mp1[mp[x]]=x;
        }
        for(int j=1; j<num; j++)
        {
            add(stop[0],stop[j]);
        }
    }
    cin >> x >> y;
    if(mp[x]==0)//就是在这个地方wrong了几遍
        mp[x]=++cnt1,mp1[mp[x]]=x;
    if(mp[y]==0)
        mp[y]=++cnt1,mp1[mp[y]]=y;
    s=mp[x],t=mp[y];
    int num=0;
    if(dfs(s)==1)
    {
        for(int i=t; i!=s; i=pre[i])
            stop[num++]=i;
        stop[num++]=mp[x];
        cout << mp1[stop[num-1]];
        for(int i=num-2;i>=0;i--)
            cout << " " << mp1[stop[i]];
        cout << endl;
    }
    else
        cout << "no route found" << endl;
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值