题目链接:
http://poj.org/problem?id=1635
Subway tree systems
Description Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, there is one and only one way of going by subway. Moreover, most of these cities have a unique central station. Imagine you are a tourist in one of these cities Input On the first line of input is a single positive integer n, telling the number of test scenarios to follow.Each test scenario consists of two lines, each containing a string of the characters ‘0‘ and ‘1‘ of length at most 3000, both describing a correct exploration Output exploration tours of the same subway tree system, or the text "different" if the two strings cannot be exploration tours of the same subway tree system. Sample Input 2 0010011101001011 0100011011001011 0100101100100111 0011000111010101 Sample Output same different Source |
[Submit] [Go Back] [Status]
[Discuss]
题目意思:
判断两棵树是否为同构的。0表示往前一步,1表示往后一步,从根出发。
解题思路:
有向树的最小表示。
对每个节点的每颗子树表示的字符串排序汇总。
代码:
//#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio> #include<sstream> #include<cstdlib> #include<string> #include<string.h> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<ctime> #include<bitset> #include<cmath> #define eps 1e-6 #define INF 0x3f3f3f3f #define PI acos(-1.0) #define ll __int64 #define LL long long #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 #define M 1000000007 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; string a,aa,bb; int n; string dfs(int i) { vector<string>myv; while(i<n&&a[i]==‘0‘) //说明是子树 { myv.push_back(‘0‘+dfs(i+1)); i+=myv.back().size(); //加上子树占用的位置,每次加一颗子树 } sort(myv.begin(),myv.end()); //所有子树排序 string res; for(int i=0;i<myv.size();i++) //连成一个整体 res+=myv[i]; return res+‘1‘; //返回到当前树 } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t; scanf("%d",&t); while(t--) { cin>>a; n=a.size(); aa=dfs(0); cin>>a; n=a.size(); bb=dfs(0); if(aa==bb) printf("same\n"); else printf("different\n"); } return 0; }
[有向树的最小表示] poj 1635 Subway tree systems,布布扣,bubuko.com