Andryusha is an orderly boy and likes to keep things in their place.
Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.
Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?
The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs.
The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely,xi means that thei-th sock Andryusha took out was from pairxi.
It is guaranteed that Andryusha took exactly two socks of each pair.
Print single integer — the maximum number of socks that were on the table at the same time.
1
1 1
1
3
2 1 1 3 2 3
2
In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.
In the second example Andryusha behaved as follows:
- Initially the table was empty, he took out a sock from pair 2 and put it on the table.
- Sock (2) was on the table. Andryusha took out a sock from pair1 and put it on the table.
- Socks (1, 2) were on the table. Andryusha took out a sock from pair1, and put this pair into the wardrobe.
- Sock (2) was on the table. Andryusha took out a sock from pair3 and put it on the table.
- Socks (2, 3) were on the table. Andryusha took out a sock from pair2, and put this pair into the wardrobe.
- Sock (3) was on the table. Andryusha took out a sock from pair3 and put this pair into the wardrobe.
水题,模拟。。。代码如下:
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int main()
{
map<int,int> m;
int n,t,ans=0;
cin>>n;
for(int i=0;i<2*n;++i)
{
cin>>t;
if(m[t]) m.erase(t);
else m[t]++;
int p=m.size();
ans=max(ans,p);
}
cout<<ans<<endl;
return 0;
}
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with(n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that ifa, b andc are distinct squares that a and b have a direct path between them, andb and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
The first line contains single integer n (3 ≤ n ≤ 2·105) — the number of squares in the park.
Each of the next (n - 1) lines contains two integersx and y (1 ≤ x, y ≤ n) — the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
In the first line print single integer k — the minimum number of colors Andryusha has to use.
In the second line print n integers, thei-th of them should be equal to the balloon color on thei-th square. Each of these numbers should be within range from1 to k.
3
2 3
1 3
3
1 3 2
5
2 3
5 3
4 3
1 3
5
1 3 2 5 4
5
2 1
3 2
4 3
5 4
3
1 2 3 1 2
In the first sample the park consists of three squares: 1 → 3 → 2. Thus, the balloon colors have to be distinct.
Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
- 1 → 3 → 2
- 1 → 3 → 4
- 1 → 3 → 5
- 2 → 3 → 4
- 2 → 3 → 5
- 4 → 3 → 5
Illustration for the second sample.
In the third example there are following triples:
- 1 → 2 → 3
- 2 → 3 → 4
- 3 → 4 → 5
Illustration for the third sample.
每一层从1开始递增枚举,只要满足当前颜色不等于父亲和爷爷,一次DFS即可,代码如下:
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=200000+5;
int col[maxn];
int sum=0;
int n,x,y;
bool vis[maxn];
vector<int> v[maxn];
bool judge(int x,int y,int z)
{
return (x!=col[y])&&(x!=col[z])&&(col[y]!=col[z]);
}
void DFS(int now,int fa)
{
int j=1;
///cout<<"now="<<now<<" fa="<<fa<<endl;
for(int i=0;i<v[now].size();++i)
{
int t=v[now][i];
if(vis[t]) continue;
vis[t]=true;
//cout<<"t="<<t<<endl;
//for(int k=1;k<=n;++k) cout<<col[k]<<" ";
//cout<<endl;
for(;j<=maxn;++j)
if(judge(j,now,fa))
{
//cout<<"color "<<j<<endl;
col[t]=j++;
if(v[t].size()) DFS(t,now);
break;
}
sum=max(sum,j-1);
}
}
int main()
{
memset(col,0,sizeof(col));
memset(vis,false,sizeof(vis));
cin>>n;
for(int i=1;i<n;++i)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
col[1]=1;
vis[1]=true;
DFS(1,0);
cout<<sum<<endl;
for(int i=1;i<=n;++i)
{
cout<<col[i];
if(i!=n) cout<<" ";
}
cout<<endl;
return 0;
}