HAVANA — It seemed like a new chapter in a long and troubled history.

Thousands of Cubans bid farewell to Fidel Castro on Monday, filing into a plaza where he often railed against American imperialism. The same morning, the first regularly scheduled flight from the United States in more than 50 years landed in Havana, a potent example of the newly opened doors between the former rivals.

But President-elect Donald J. Trump warned on Monday that the push to build ties with Cuba after decades of animosity could quickly be wiped away.

“If Cuba is unwilling to make a better deal for the Cuban people, the Cuban/American people and the U.S. as a whole, I will terminate deal,” he said on Twitter.

Mr. Trump’s message threatened to end one of President Obama’s signature foreign policy initiatives. Mr. Obama’s moves to relax restrictions on commerce, trade and financial transactions with Cuba were never part of a single “deal,” but rather a decision that engagement with the island nation would bring more change than decades of isolation.