代码丑陋,不想改了
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<string,LL>PII;
const int maxn=100;
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
return a/gcd(a,b)*b;
}
int main()
{
int n;
scanf("%d",&n);
char s[maxn];
vector<PII>str;
str.clear();
LL a=0,b=1;
for (int i=0;i<n;i++)
{
scanf("%s",s);
if(s[0]=='-') {
if(s[1]=='0') continue;
}
else if(s[0]=='0') continue;
int len=strlen(s),x;
for(int j=0;j<len;j++)
if(s[j]=='/') {
x=j;
break;
}
LL k=0;
for(int j=x+1;j<len;j++)
k=k*10+s[j]-'0';
b=lcm(b,k);
str.push_back(PII(string(s),k));
}
for(int i=0;i<str.size();i++)
{
string k=str[i].first;
if(k[0]=='-') {
LL x=0;
for(int j=1;j<k.length();j++)
{
if(k[j]=='/') break;
x=x*10+k[j]-'0';
}
x*=(b/str[i].second);
a-=x;
}
else {
LL x=0;
for(int j=0;j<k.length();j++)
{
if(k[j]=='/') break;
x=x*10+k[j]-'0';
}
x*=(b/str[i].second);
a+=x;
}
}
if(a>0) {
LL k=gcd(a,b);
a/=k;
b/=k;
LL x=a/b;
bool flag=false;
if(x) {
printf("%lld",x);
a-=x*b;
flag=true;
}
if(a&&flag) printf(" %lld/%lld",a,b);
else if(a&&!flag) printf("%lld/%lld",a,b);
printf("\n");
}
else if(a==0) {
printf("0\n");
}
else {
printf("-");
a=-a;
LL k=gcd(a,b);
a/=k;
b/=k;
LL x=a/b;
bool flag=false;
if(x) {
printf("%lld",x);
a-=x*b;
flag=true;
}
if(a&&flag) printf(" %lld/%lld\n",a,b);
else if(a&&!flag) printf("%lld/%lld",a,b);
printf("\n");
}
return 0;
}
//并查集+树的直径
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
const int maxv=1e5+5;
int head[maxv],cnt=0;
set<int>solve;
struct Edge{
int from,to,next;
};
Edge edges[maxv];
void add_edge(int u,int v)
{
edges[++cnt].next=head[u];
edges[cnt].from=u;
edges[cnt].to=v;
head[u]=cnt;
}
bool done[maxv];
int parent[maxv],n;
void init()
{
for(int i=1;i<=n;i++)
parent[i]=i;
}
int find(int x)
{
return x==parent[x]?x:parent[x]=find(parent[x]);
}
void bfs(int st)
{
queue<PII>pq;
while(!pq.empty()) pq.pop();
pq.push(PII(st,0));
memset(done,false,sizeof(done));
int m=-1;
while(!pq.empty())
{
PII k=pq.front();
pq.pop();
if(done[k.first]) continue;
done[k.first]=true;
if(m<=k.second) {
if(m<k.second) {
solve.clear();
m=k.second;
}
solve.insert(k.first);
}
for(int i=head[k.first];i!=0;i=edges[i].next)
{
if(!done[edges[i].to]) {
pq.push(PII(edges[i].to,k.second+1));
}
}
}
}
void bfs1(int st)
{
queue<PII>pq;
while(!pq.empty()) pq.pop();
pq.push(PII(st,0));
memset(done,false,sizeof(done));
int m=-1;
while(!pq.empty())
{
PII k=pq.front();
pq.pop();
if(done[k.first]) continue;
done[k.first]=true;
if(m<=k.second) {
if(m<k.second) {
solve.clear();
m=k.second;
}
solve.insert(k.first);
}
for(int i=head[k.first];i!=0;i=edges[i].next)
{
if(!done[edges[i].to]) {
pq.push(PII(edges[i].to,k.second+1));
}
}
}
}
int main()
{
solve.clear();
memset(head,0,sizeof(head));
scanf("%d",&n);
init();
bool flag=true;
for(int i=0;i<n-1;i++)
{
int v1,v2;
scanf("%d%d",&v1,&v2);
add_edge(v1,v2);
add_edge(v2,v1);
int p1=find(v1);
int p2=find(v2);
if(p1==p2) {
flag=false;
}
parent[p1]=p2;
}
int x=0;
for(int i=1;i<=n;i++)
{
int p1=find(i);
if(p1==i) x++;
}
if(!flag||x!=1) {
printf("Error: %d components\n",x);
}
else {
//树的直径
bfs(1);
set<int>::iterator it;
it=solve.begin();
int st=(*it),pos[maxv],k=0;
for(;it!=solve.end();it++)
{
pos[k++]=(*it);
}
solve.clear();
bfs1(st);
for(int i=0;i<k;i++)
solve.insert(pos[i]);
it=solve.begin();
for(;it!=solve.end();it++)
printf("%d\n",(*it));
solve.clear();
}
return 0;
}