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

*/
时间: 2024-10-11 05:59:53

ZOJ 3830 Alkanes 大模拟!!的相关文章

AC日记——神奇的幻方 洛谷 P2615(大模拟)

题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第一行的中间. 之后,按如下方式从小到大依次填写每个数K(K=2,3,…,N*N): 1.若(K−1)在第一行但不在最后一列,则将K填在最后一行,(K−1)所在列的右一列: 2.若(K−1)在最后一列但不在第一行,则将K填在第一列,(K−1)所在行的上一行: 3.若(K−1)在第一行最后一列,则将K填在(K−1)

URAL 1715. Another Ball Killer (大模拟)

1715. Another Ball Killer Time limit: 2.0 second Memory limit: 64 MB Contestants often wonder what jury members do during a contest. Someone thinks that they spend all the contest fixing bugs in tests. Others say that the jury members watch the conte

POJ 1835 大模拟

宇航员 #include<iostream> #include<cstdio> #include<string> #include<cstring> #define maxn 10010 using namespace std; int a[7],temp[7]; char str[10]; void solve(int str2[],int str3[]) { if(strcmp(str,"forward")==0)//方向不变 { s

大模拟祭

考试的前一天晚上我还在和$letong$说,我以后晚上再颓就去打模拟,然后就考了个大模拟 我比较$sb$,各种情况全分开了,没怎么压行,打了$1000$行整,$30$多$k$,妈妈再也不怕我打不出来猪国杀了 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 int n,len,Std; 6 int b[5]; 7 char s[20],ss[20];

[ACM] ZOJ 3844 Easy Task (模拟+哈希)

Easy Task Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. And then you should replace both a and bwith a-b. Yo

zoj 3826 Hierarchical Notation(模拟)

题目链接:zoj 3826 Hierarchical Notation 题目大意:给定一些结构体.结构体有value值和key值,Q次询问,输出每一个key值相应的value值. 解题思路:思路非常easy.写个类词法的递归函数,每次将key值映射成一个hash值,用map映射每一个key的value起始终止位置,预处理完了查询就非常easy了. 这题是最后10分钟出的.由于没有考虑value为{}的情况,导致RE了.可是zoj上显示的是SE,表示不理解什么意思,事实上就是RE.只是另一个地方会

HDU 1986 &amp; ZOJ 2989 Encoding(模拟)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1986 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1988 HDU 1987 & ZOJ 2990 和这题刚好相反,也是比较容易模拟: Chip and Dale have devised an encryption method to hide their (written) text messages

ZOJ How Many Nines 模拟 | 打表

How Many Nines Time Limit: 1 Second      Memory Limit: 65536 KB If we represent a date in the format YYYY-MM-DD (for example, 2017-04-09), do you know how many 9s will appear in all the dates between Y1-M1-D1 and Y2-M2-D2 (both inclusive)? Note that

2017&quot;百度之星&quot;程序设计大赛 - 复赛1001&amp;&amp;HDU 6144 Arithmetic of Bomb【java大模拟】

Arithmetic of Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 129    Accepted Submission(s): 94 Problem Description 众所周知,度度熊非常喜欢数字. 它最近在学习小学算术,第一次发现这个世界上居然存在两位数,三位数……甚至N位数! 但是这回的算术题可并不简单,由于