Problem Description
After June 1st, elementary students of Ted Land are still celebrating “The Sacred Day of Elementary Students”. They go to the streets and do some elementary students stuff. So we call them “elementary students”. There are N cities in Ted Land. But for simplicity, the mayor Matt only built N - 1 roads so that all cities can reach each other. Some of the roads are occupied by the “elementary students”. They will put an celebration hat on everyone who goes through the road without one. But if someone goes through the road with a celebration hat on his head, “elementary students” will steal the hat for no reason. Since Matt doesn’t have a celebration hat, he wants to know how many different paths in his land that he can end up with a hat. Two paths are considered to be different if and only if they have different start city or end city. As the counsellor of the mayor Matt, you have to answer this question for him. The celebration elementary students are not stable: sometimes a new crowd of elementary students go to an empty road; sometimes the elementary students on a road will go back home and remain the road empty. Matt will send you the monitor about the change of elementary students on road and ask you the question above. You will be fired if you answer wrong.
Input
The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.
For each test case, the first line contains N (1<=N<=30000) describing the number of cities.
Then N lines follow. The ith line of these contains the name of the ith city, it’s a string containing only letters and will not be longer than 10.
The following N - 1 lines indicate the N - 1 edges between cities. Each of these lines will contain two strings for the cities’ name and a integer for the initial status of the edge (0 for empty, 1 for crowded).
Then an integer M (1<=M<=60000) describes the number of queries. There are two kinds of query:
● “M i” means the status changing of the ith (starting from 1) road (0 to 1, 1 to 0);
● “Q” means that Matt asks you the number of different paths.
Output
For each test case, first output one line “Case #x:”, where x is the case number (starting from 1).
Then for each “Q” in input, output a line with the answer.
Sample Input
1
5
a
b
c
d
e
a b 1
b c 0
c d 1
d e 1
7
Q
M 1
Q
M 3
Q
M 4
Q
Sample Output
Case #1:
12
8
8
0
这道题我写了三个半小时!!!!!总是有奇奇怪怪的错误!!!!而且中间的时候用链式前向星建图然后错了一个多小时……然后我借鉴了别人的建图方式(:з」∠)_( ๑ŏ ﹏ ŏ๑ )(⋟﹏⋞)
˚‧º·(˚ ˃̣̣̥᷄⌓˂̣̣̥᷅ )‧º·˚
题意:给定一棵边权为 0/1 的树,要求支持两类操作:1. 询问有多少条路径 xor 和为 1。2. 修改一条边的权值。
思路:用a[x,root]表示x到根节点的路径上的异或和,对于某两个点,x和y以及根节点root来说,a[x,y]=a[x,root]^a[y,root]。所以可以用线段树维护每个点到根结点的异或和,用sum来记录线段树[l,r]区间的点到根节点异或和为1的个数。求所有异或和为1的路径数=到根节点异或和为0的数×到根节点异或和为1的数×2。(0,1组合方式)
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn=30005;
struct edge
{
int u,v,w;
edge(int u,int v,int w)
{
this->u=u;
this->v=v;
this->w=w;
}
};
int head[maxn];
vector ed;
vector<int>g[maxn];
map<string,int>mp;
int n,m,cnt;
int s[maxn],e[maxn],dfsnum[maxn],dep[maxn];
int a[maxn];
int ti;
struct Tree{
int l, r, sum, add;
}tree[maxn<<2];
//g[u]存的是和u相连的边
void add(int u,int v,int w)
{
ed.push_back(edge(u,v,w));
ed.push_back(edge(v,u,w));
int k=ed.size();
g[u].push_back(k-2);
g[v].push_back(k-1);
}
void dfs(int u,int fa,int xo)
{
s[u]=++ti;
dfsnum[ti]=u;
a[ti]=xo;
dep[u]=dep[fa]+1;
for(int i=0;iint j=g[u][i];
if(ed[j].v==fa) continue;
dfs(ed[j].v,u,xo^ed[j].w);
}
e[u]=ti;
}
//延迟更新
void pushup(int root){//儿子把信息传递给父亲
//if(tree[root].l==tree[root].r)return ;
tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum; //<<1:*2; <<1|1:*2+1
return ;
}
void pushdown(int root){//父亲把自己的信息传给儿子
if(tree[root].l==tree[root].r)return ;
if(tree[root].add==0)return ;
tree[root<<1].add^=1;
tree[root<<1|1].add^=1;
tree[root<<1].sum=(tree[root<<1].r-tree[root<<1].l+1)-tree[root<<1].sum;
tree[root<<1|1].sum=(tree[root<<1|1].r-tree[root<<1|1].l+1)-tree[root<<1|1].sum;
tree[root].add=0;
return ;
}
//初始化
void build(int l,int r,int root){
tree[root].l=l;
tree[root].r=r;
tree[root].sum=0;
tree[root].add=0;
if(l==r){//直到单个区间位置
tree[root].sum=a[l];
tree[root].add=0;
return;
}
int mid=(l+r)>>1;
build(l,mid,root<<1);//左区间
build(mid+1,r,root<<1|1); //右区间
pushup(root);//把儿子的信息更新到父亲
return;
}
//更新区间
void update(int l,int r,int z,int root){
if(l<=tree[root].l&&tree[root].r<=r){
tree[root].sum=(tree[root].r-tree[root].l+1)-tree[root].sum;
tree[root].add^=1;//把要更新的内容保存下来,等到要用儿子时 再去更新
return ;
}
pushdown(root);//用父亲的信息更新儿子
int mid=tree[root].l+tree[root].r>>1;
if(r<=mid)update(l,r,z,root<<1);
else if(l>mid)update(l,r,z,root<<1|1);
else {
update(l,mid,z,root<<1);
update(mid+1,r,z,root<<1|1);
}
pushup(root);//更新父亲
return ;
}
//查找区间信息
int query(int l,int r,int root){
if(l==tree[root].l&&tree[root].r==r){
return tree[root].sum;
}
pushdown(root);
int mid=tree[root].l+tree[root].r>>1;
if(r<=mid)return query(l,r,root<<1);
else if(l>mid)return query(l,r,root<<1|1);
else return query(l,mid,root<<1)+query(mid+1,r,root<<1|1);
}
int main()
{
int T;
cnt=1;
scanf("%d",&T);
while(T--)
{
printf("Case #%d:\n",cnt++);
memset(s,0,sizeof(s));
memset(e,0,sizeof(e));
ed.clear();
scanf("%d",&n);
string ss;
for(int i=1;i<=n;i++)
{
cin>>ss;
mp[ss]=i;
//cout<
}
ti=0;
for(int i=1;i<=n;i++) g[i].clear();
for(int i=1;istring ss,t;
int u,v,w;
cin>>ss>>t;
u=mp[ss]; v=mp[t];
scanf("%d",&w);
//cout<
add(u,v,w);
}
dep[0]=0;
dfs(1,0,0);
build(1,n,1);
scanf("%d",&m);
while(m--)
{
char od[5];
scanf("%s",od);
if(od[0]=='M')
{
int x;
scanf("%d",&x);
x--;
x <<=1;
int u,v;
u=ed[x].u; v=ed[x].v;
//这里要注意修改的是离根结点更远的那个点到根节点的异或和值
if(dep[u]1 ,1);
}
else
{
int x=query(1,n,1);
ll ans=(ll)x*(n-x)*2;
printf("%lld\n",ans);
}
}
}
return 0;
}