首页 > 代码库 > ZOJ 3830 Alkanes 大模拟!!

ZOJ 3830 Alkanes 大模拟!!

题目链接:点击打开链接

就是给了一个树形的烷烃,输出他的科学命名

然后写写写。。。

==强行回忆高中化学

#include<stdio.h>
#include <iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include <string>
#include<queue>
#include<vector>
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x <0) {
		putchar('-');
		x = -x;
	}
	if (x>9) pt(x / 10);
	putchar(x % 10 + '0');
}
const int N = 50;
using namespace std;
string name[N] = { "ewrt", "meth", "eth", "prop", "but", "pent", "hex", "hept", "oct", "non", "dec", "undec", "dodec", "tridec", "tetradec", "pentadec" };
vector<int>G[N];
struct hehe{
	int dis[N], pre[N], id, sum;
	vector<int>D[N];
	int dfs(int u, int fa){
		int siz = 1;
		for (int i = 0; i < G[u].size(); i++)
		{
			int v = G[u][i];
			if (v != fa)
				siz += dfs(v, u);
		}
		return siz;
	}
	int bfs(int s, int t){
		memset(dis, -1, sizeof dis);
		memset(pre, -1, sizeof pre);
		dis[s] = 1;
		queue<int>q; q.push(s);
		while (!q.empty()){
			int u = q.front(); q.pop();
			for (int i = 0; i < G[u].size(); i++)
			{
				int v = G[u][i];
				if (dis[v] != -1)continue;
				dis[v] = dis[u] + 1;
				pre[v] = u;
				q.push(v);
			}
		}
		int last = -1, val = 0;
		while (t != -1){
			for (int i = 0; i < G[t].size(); i++)
			{
				int v = G[t][i];
				if (v != last && v != pre[t]){
					if (id == -1 || id > dis[t]) id = dis[t];
					sum += dis[t];
					D[dfs(v, t)].push_back(dis[t]);
				}
			}
			last = t;
			t = pre[t];
		}
	}
	int T;
	void work(int s, int t){
		T = t;//   printf("    %d->%d\n", s, t);
		id = -1;
		sum = 0;
		for (int i = 0; i < N; i++)D[i].clear();
		bfs(s, t);
		for (int i = 0; i < N; i++)sort(D[i].begin(), D[i].end());
		/*      for(int i = 0; i <N; i++)if(D[i].size()){printf("%d: ", i);for(int j = 0; j < D[i].size(); j++)printf("%d ", D[i][j]);puts("");}*/
	}
	int big(int x, int y){
        if(D[x].size()!=D[y].size())return D[x].size()<D[y].size();
        return x<y;
	}
	void put(){
		int f = 0;
		bool vis[N]; memset(vis,0,sizeof vis);
		for (int i , tim = 1; tim < N; tim++) {
            int pos = -1;
            for(int j = 1; j < N; j++)
                if(vis[j]==0 && D[j].size() && (pos == -1 || big(j,pos)))
                    pos = j;
            if(pos == -1)break;
            vis[pos] = 1;
            i = pos;
            if (f++)       putchar('-');
            for (int j = 0; j < D[i].size(); j++)
            {
                if (j)printf(",");
                printf("%d", D[i][j]);
            }
            putchar('-');
            if (2 == (int)D[i].size())
                printf("di");
            else if (3 == (int)D[i].size())
                printf("tri");
            else if (4 == (int)D[i].size())
                printf("tetra");
            else if ((int)D[i].size() > 4){
                cout << name[D[i].size()];
                printf("a");
            }
            cout << name[i];
            printf("yl");
        }
		cout << name[dis[T]];
		puts("ane");
	}
}b[2];
int dis[N], pre[N];
int bfs(int s, int t){
	memset(dis, -1, sizeof dis);
	memset(pre, -1, sizeof pre);
	dis[s] = 0;
	queue<int>q; q.push(s);
	while (!q.empty()){
		int u = q.front(); q.pop();
		for (int i = 0; i < G[u].size(); i++)
		{
			int v = G[u][i];
			if (dis[v] != -1)continue;
			dis[v] = dis[u] + 1;
			pre[v] = u;
			q.push(v);
		}
	}
	int last = -1, val = 0;
	while (t != -1){
		for (int i = 0; i < G[t].size(); i++)
		{
			int v = G[t][i];
			if (v != last && v != pre[t])val++;
		}
		last = t;
		t = pre[t];
	}
	return val;
}
int n, m, len, a[2], siz;
void find_total(){
	len = siz = 0;
	for (int i = 1; i <= n; i++)
	for (int j = i + 1; j <= n; j++)
	{
		int tmp = bfs(i, j);
		if (dis[j] > len || (dis[j] == len && tmp > siz)){
			len = dis[j]; a[0] = i; a[1] = j;
			siz = tmp;
		}
	}
///	printf("%d->%d\n", a[0], a[1]);
}
void input(){
	for (int i = 0; i < N; i++)G[i].clear();
	int u, v;
	while (m--){
		scanf("%d %d", &u, &v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
}
int main() {
	while (~scanf("%d %d", &n, &m)){
		input();
		find_total();
		b[0].work(a[0], a[1]);
		b[1].work(a[1], a[0]);
		if (b[0].id<b[1].id)
			b[0].put();
		else if (b[0].id>b[1].id)
			b[1].put();
		else
		{
			if (b[0].sum < b[1].sum)
				b[0].put();
			else if(b[0].sum > b[1].sum)
				b[1].put();
            else
            {
                int id = -1;
                for(int i = N-1; id==-1 && i >= 0; i--)
                {
                    if(b[0].D[i].size()>b[1].D[i].size())
                        id = 0;
                    else if(b[0].D[i].size()<b[1].D[i].size())
                        id = 1;
                    else
                    {
                        for(int j = 0; j < b[0].D[i].size(); j++)
                            if(b[0].D[i][j]<b[1].D[i][j])
                                id = 0;
                            else if(b[0].D[i][j]>b[1].D[i][j])
                                id = 1;
                    }
                }
                if(id==-1)id=0;
                b[id].put();
            }
		}
	//	b[0].put(); b[1].put();
	}
	return 0;
}
/*
10 9
1 2
2 3
2 4
4 5
4 6
4 9
9 10
6 8
6 7

2,3,4-trimethyl-3-ethylpentane

10 9
1 2
2 5
2 6
2 3
3 7

3 8
3 4
8 9
8 10

12 11
1 2
2 5
2 6
2 3
3 7

7 11
3 8
3 4
8 9
8 10

7 12

14 13
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
2 10
8 11
4 13
13 14
6 12

14 13
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
3 10
7 11
4 13
10 14
6 12

*/


ZOJ 3830 Alkanes 大模拟!!