結果

問題 No.439 チワワのなる木
ユーザー 小夫
提出日時 2025-03-30 14:31:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 963 bytes
コンパイル時間 2,227 ms
コンパイル使用メモリ 199,820 KB
実行使用メモリ 19,396 KB
最終ジャッジ日時 2025-03-30 14:31:18
合計ジャッジ時間 4,481 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:49:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   49 |         scanf("%s",s+1);
      |         ~~~~~^~~~~~~~~~

ソースコード

diff #

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch&15);
		ch=getchar();
	}
	return x*f;
}
inline void write(int x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)write(x/10);
	putchar(x%10+'0');
	return;
}
char s[100010];
vector<int>mp[200010];
int ans,f[100010][4];
inline void dfs(int pos,int fa)
{
	bool fg=(s[pos]=='w');
	f[pos][fg]=1;
	for(auto to:mp[pos])
	{
		if(fa==to)continue;
		dfs(to,pos);
		for(int i=0;i<4;++i)ans+=f[pos][i]*f[to][3-i],f[pos][i]+=f[to][i];
		if(fg)for(int i=0;i<2;++i)f[pos][i+2]+=f[to][i];
	}
}
signed main()
{
	int n=read();
	scanf("%s",s+1);
	for(int i=1;i<n;++i)
	{
		int u=read(),v=read();
		mp[u].emplace_back(v);
		mp[v].emplace_back(u);
	}
	dfs(1,0);
	write(ans);
	return 0;
}

0